Skip to content

Instantly share code, notes, and snippets.

@lahmatiy
Last active August 29, 2015 14:02
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 lahmatiy/3d97ee23f3d89941970f to your computer and use it in GitHub Desktop.
Save lahmatiy/3d97ee23f3d89941970f to your computer and use it in GitHub Desktop.
Closure vs Function#bind
//
// Closure
//
var a = []; // хранилище истансов, чтобы они не разрушались (не собирались GC)
var getClosure = function(a){ return function(b){ return fn(a, b); } };
var t = performance.now();
for (var i = 0; i < 10000; i++)
a.push(getClosure(i));
console.log(performance.now() - t);
//
// Function#bind
//
var a = [];
var fn = function(a,b){ return a + b; };
var t = performance.now();
for(var i = 0; i < 10000; i++)
a.push(fn.bind(null, i));
console.log(performance.now() - t);

Closure

  • Chrome 35: 14ms / 640kb
  • Firefox 30: 10.3ms
  • IE 11: 9.3ms / 520Kb

Function#bind

  • Chrome 35: 28ms / 2 080Kb
  • Firefox 30: 17.1ms
  • IE 11: 2.9ms / 680Kb
@azproduction
Copy link

Короткая какая-то функция. Сделай реальную хотя бы строк на 20 :) Может тогда по памяти выиграем.

@lahmatiy
Copy link
Author

Реальная это как раз та что fn - а в тесте создаем для нее обертки.

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