【发布时间】:2011-09-24 10:58:46
【问题描述】:
在javascript中编写如下函数有两种功能等效的方式,哪个更好或更高效,为什么?
(str) ->
s = 0
for i in [0...str.length]
s += str.charCodeAt i
s
或
(str) ->
s = 0
for i in str
s += i.charCodeAt 0
s
顺便说一句:您能建议任何其他方法吗?
编辑:根据 JSPerf,第一个更快:http://jsperf.com/coffee-for-loop-speed-test - 这是为什么?
【问题讨论】:
-
大约快 1 微秒(在我的机器上),我会给你...(0.533ms vs 1.64ms)
标签: javascript loops coffeescript