Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up by avoiding arguments and apply #55

Merged
merged 1 commit into from
Sep 12, 2016
Merged

Speed up by avoiding arguments and apply #55

merged 1 commit into from
Sep 12, 2016

Conversation

RubenVerborgh
Copy link
Contributor

This pull request avoids passing arguments around, which is bad for performance. It also avoids unnecessary apply calls by optimizing for common numbers of parameters, as in Node.js.

// This function accepts the same arguments as setImmediate, but
// returns a function that requires no arguments.
function partiallyApplied(handler) {
var args = [].slice.call(arguments, 1);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@domenic
Copy link
Collaborator

domenic commented Aug 15, 2016

Benchmarks before and after, please.

} else {
var task = tasksByHandle[handle];
if (task) {
currentlyRunningATask = true;
try {
task();
run(task);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Separate function, outside of try finally for performance.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ha fun fact from testing, try/finally is not an optimization killer...in v8, it is one in firefox

@RubenVerborgh
Copy link
Contributor Author

Benchmarks for the same case in Node.js are here; will add some for this instance as well.

@RubenVerborgh
Copy link
Contributor Author

RubenVerborgh commented Aug 15, 2016

Benchmark

time on Node 6.3.1 (s)
native setImmediate 7.3
current master 16.2
this pull request 2.3

@domenic
Copy link
Collaborator

domenic commented Aug 15, 2016

Awesome. Will merge and publish some time when I'm not at work.


function setImmediate(callback) {
// Callback can either be a function or a string
if (typeof callback !== 'function')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

curly braces around the body please

This avoids overhead with `arguments` and `apply`.
@RubenVerborgh
Copy link
Contributor Author

RubenVerborgh commented Aug 16, 2016

Updated code, passes npm lint now (perhaps you want to update the Hound check).

@domenic domenic merged commit aa63d91 into YuzuJS:master Sep 12, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants