fast-js,各种 benchmark,测出最快的 js 写法
发布于 2天前 作者 alsotang 189 次浏览 来自 分享

github 地址:https://github.com/alsotang/fast-js 大家如果有其他的 benchmark 要贡献的话,欢迎提 issue 或者在此留言给我。

fast-js

:heart_eyes: Writing Fast JavaScript

env

  • platform: OS X 10.10.4
  • cpu: 1.4 GHz Intel Core i5
  • iojs: v2.3.0
  • v8: 4.2.77.20

benchmark

arguments_to_array.js

arguments_to_array
  [].slice.apply ................................. 3,846,459 op/s
  [].slice.call .................................. 3,718,698 op/s
  Array.prototype.slice.apply .................... 3,240,713 op/s
  Array.prototype.slice.call ..................... 4,390,122 op/s
  lodash.toArray ................................. 13,271,549 op/s

clone_object.js

clone_object
  JSON.parse(JSON.stringify) ..................... 192,176 op/s
  _.cloneDeep .................................... 151,387 op/s
  _.clone. this is shadow clone .................. 918,130 op/s

for_loop.js

for_loop
  normal for loop. i < arr.length ................ 7,143 op/s
  normal for loop. cache arr.length .............. 7,070 op/s
  native forEach ................................. 145 op/s
  lodash.forEach ................................. 656 op/s

hidden_class.js

hidden_class
  withoutHiddenClass ............................. 72,448,120 op/s
  withHiddenClass ................................ 130,504,857 op/s

inner_function.js

inner_function
  inner .......................................... 23,634,031 op/s
  outter ......................................... 94,173,853 op/s

iterate_object.js

iterate_object
  for .. in .. ................................... 19,144,156 op/s
  Object.keys .................................... 1,858,462 op/s
  lodash.forEach ................................. 863,574 op/s

map_loop.js

map_loop
  normal loop. use push .......................... 796 op/s
  normal loop. use index ......................... 1,223 op/s
  new Array(arr.length) .......................... 3,539 op/s
  native map ..................................... 132 op/s
  lodash.forEach ................................. 611 op/s

new_array.js

new_array
  new Array() .................................... 58,084,959 op/s
  [] ............................................. 162,278,430 op/s

next_tick.js

next_tick
  process.nextTick ............................... 2,315 op/s
  setTimeout(0) .................................. 1,938 op/s
  setImmediate ................................... 1,349 op/s

start_with.js

start_with
  regex /^ab/ .................................... 6,753,906 op/s
  indexOf === 0 .................................. 12,967,224 op/s
  lodash.startsWith .............................. 14,404,678 op/s

str_concat.js

str_concat
  + .............................................. 609,026,862 op/s
  += ............................................. 121,310,295 op/s
  arr.join("") ................................... 3,004,308 op/s
  str.concat ..................................... 31,938,318 op/s

str_to_int_number.js

str_to_int_number
  +str ........................................... 18,200,137 op/s
  Number(str) .................................... 35,489,770 op/s
  parseInt(str) .................................. 42,522,640 op/s
  parseInt(str, 10) .............................. 63,504,924 op/s
  ~~str .......................................... 17,663,670 op/s
  str | 0 ........................................ 19,359,115 op/s
  str >> 0 ....................................... 20,045,162 op/s
  str >>> 0 ...................................... 18,191,777 op/s
  str * 1 ........................................ 20,565,845 op/s

contribute

  1. add your test to benchmark dir
  2. run $ make build and it will update README.md including new test
6 回复

iojs自己好像就有基准测试吧

@i5ting 没看过…不过很多 js 性能的文章都会提到一些性能优化的点,我就整理在一起了。

+arr.join快200倍,一般不都推荐用arr.join么,不过不做大量的字符串拼接,这个时间差距应该可以忽略不计的哈

@luoyjx 一般都推荐用 + 的。用 join 那是老的 js 引擎优化了,现在反而 + 比较高。

@alsotang https://github.com/airbnb/javascript/tree/master/es5#strings 我是看到这里这么说的,不过他说针对ES5

When programmatically building up a string, use Array#join instead of string concatenation. Mostly for IE: jsPerf.

难道是考虑到IE的原因 =。=

nodejs 有benchmark的模块吗?

回到顶部