Skip to content

Instantly share code, notes, and snippets.

@thurt
Last active February 27, 2016 16:47
Show Gist options
  • Save thurt/00f0c6d8ea56d1be1b15 to your computer and use it in GitHub Desktop.
Save thurt/00f0c6d8ea56d1be1b15 to your computer and use it in GitHub Desktop.
bind vs call or apply
<script>
Benchmark.prototype.setup = function() {
var emptyFn = function(str, num) {}
var str = 'test string'
var num = 1
var preBoundFn = emptyFn.bind(null, str, num)
};
Benchmark.prototype.teardown = function() {
emptyFn = null
str = null
num = null
preBoundFn = null
};
</script>
Testing in Chrome 48.0.2564.116 on Windows XP
Test Ops/sec
using bind once
var boundFn = emptyFn.bind(null, str, num)
boundFn()
152,644
±1.56%
100% slower
using call many times
emptyFn.call(null, str, num)
44,251,653
±2.11%
fastest
using apply many times
emptyFn.apply(null, [str, num])
5,948,666
±2.10%
87% slower
using preBoundFn
preBoundFn()
494,834
±1.71%
99% slower
@thurt
Copy link
Author

thurt commented Feb 27, 2016

A conclusion: Using #apply many times is an order of magnitude faster than using a pre-bound function many times -- so it seems in most use cases you will want to use #apply with the original function reference instead of creating a copy with #bind.

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