Skip to content

Instantly share code, notes, and snippets.

@jeremychone
Last active April 26, 2021 21:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremychone/9a16ef82d4243053cb2a81ac0aee5117 to your computer and use it in GitHub Desktop.
Save jeremychone/9a16ef82d4243053cb2a81ac0aee5117 to your computer and use it in GitHub Desktop.
// test
var math = {
base: 0,
add: add
};
function add(a, b) {
return this.base + a + b;
}
var l = 10000000;
var esBoundAdd = add.bind(math);
var customBoundAdd = customBind(add, math);
function direct() {
var now = Date.now(), end, r;
var vals = [1, 1];
for (var i = 0; i < l; i++) {
vals[0] = vals[1] = i;
r = math.add(i, i);
}
end = Date.now();
console.log("direct result:", r, "time:", (end - now));
}
function call() {
var now = Date.now(), end, r;
var vals = [1, 1];
for (var i = 0; i < l; i++) {
vals[0] = vals[1] = i;
r = math.add.call(math, i, i);
}
end = Date.now();
console.log("call result:", r, "time:", (end - now));
}
function apply() {
var now = Date.now(), end, r;
var vals = [1, 1];
for (var i = 0; i < l; i++) {
vals[0] = vals[1] = i;
r = math.add.apply(math, vals);
}
end = Date.now();
console.log("apply result:", r, "time:", (end - now));
}
function esBound() {
var now = Date.now(), end, r;
var vals = [1, 1];
for (var i = 0; i < l; i++) {
vals[0] = vals[1] = i;
r = esBoundAdd(i, i);
}
end = Date.now();
console.log("esBound result:", r, "time:", (end - now));
}
function customBound() {
var now = Date.now(), end, r;
var vals = [1, 1];
for (var i = 0; i < l; i++) {
vals[0] = vals[1] = i;
r = customBoundAdd(i, i);
}
end = Date.now();
console.log("customBound result:", r, "time:", (end - now));
}
function customBind(fn, ctx) {
return function () {
return fn.apply(ctx, arguments);
};
}
direct();
call();
apply();
esBound();
customBound();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment