Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@aperezdc
Created December 3, 2014 19:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aperezdc/b1b5dab48eeb1dcfadc8 to your computer and use it in GitHub Desktop.
Save aperezdc/b1b5dab48eeb1dcfadc8 to your computer and use it in GitHub Desktop.
Compare speed of f.call(...) vs. %_CallFunction(..., f) in V8
// Run with: d8 --allow-natives-syntax callfunction.js
function f1(n, cb, thisArg) {
for (var i = 0; i < n; i++) {
cb.call(thisArg, i);
}
}
function f2(n, cb, thisArg) {
for (var i = 0; i < n; i++) {
%_CallFunction(thisArg, i, cb);
}
}
var start;
var end;
function callback(i) { /* no-op */ }
start = new Date().getTime();
f1(10000000, callback, 42);
end = new Date().getTime();
print("f1", end - start, "ms");
start = new Date().getTime();
f2(10000000, callback, 42);
end = new Date().getTime();
print("f2", end - start, "ms");
@aperezdc
Copy link
Author

aperezdc commented Dec 3, 2014

Typical output in my machine:

% d8 --allow-natives-syntax callfunction.js
f1 470 ms
f2 48 ms
%

Using %_CallFunction() is consistently an order of magnitude faster.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment