【发布时间】:2012-03-04 04:08:39
【问题描述】:
最近我读到了这个performance guide Let's make the web faster,对“避免使用闭包的陷阱”的建议感到困惑(好像这些建议是为变量范围是动态的 CommonLisp 用户提供的):
var a = 'a'; function createFunctionWithClosure() { var b = 'b'; return function () { var c = 'c'; a; b; c; }; } var f = createFunctionWithClosure(); f();在调用
f时,引用a比引用b慢,后者比引用c慢。
很明显,引用局部变量 c 比 b 快,但是如果正确编写了迭代器(没有动态范围 - 类似于链式哈希表查找..)速度差异应该只是微不足道的。还是不行?
【问题讨论】:
标签: javascript performance functional-programming closures interpreter