fast-js,各种 benchmark,测出最快的 js 写法
发布于 9 年前 作者 alsotang 6995 次浏览 最后一次编辑是 8 年前 来自 分享

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
13 回复

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的模块吗?

str_concat.js

var c = '';
c += longstra;
c += longstrb;

//改成下面试试呢
var c = longstra;
c += longstrb;

我以为你要帖的是这个: https://github.com/codemix/fast.js

光 Benchmark ,最好还要补上说明为什么快,不然人云亦云,流出一些莫名其妙的性能优化方法,害处不小,比如我就听人说过 while 比 for循环快。Benchmark 方法也最好严谨些,两个不一样的东西不便放在一起比较,+str 、 str | 0 、 str * 1 这三者一样吗?

@JexCheng 这三者中的前两者,都是把 str 转成 int number 的常见用法吧

@JexCheng 现在只保留这几种了

  +str ........................................... 23,294,152 op/s
  ~~str .......................................... 24,858,884 op/s
  Number(str) .................................... 46,948,016 op/s
  parseInt(str) .................................. 41,145,945 op/s
  parseInt(str, 10) .............................. 61,879,685 op/s

@alsotang +str 、 str *1 倒是一样的,都是 ToNumber,支持16进制,支持小数、当然也支持 +“Infinity”(所以parseInt当然不支持这些值,应该拿parseFloat来比,但parseFloat又不支持16进制) 。 str|0 不但只支持整数,而且是只支持ToInt32,大了会overflow的。同样 ~~str 这些位操作都是只支持 Int32,~~"2147483648" 就溢出了

@JexCheng 了解的,前不久刚优化过一个涉及 js 中 int32 算 hash 的库。https://github.com/node-modules/node-murmurhash/pull/10/files

但确实这都是常见用法,比较一下的意义还是有的。就是注释太少哈哈

@alsotang 如果自己实现buffer的话要用join,不能用+,不过我一直用join的。。。

回到顶部