Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.