😍 Writing Fast JavaScript
each file has its own comment that illustrate why faster
- platform: OS X 10.10.4
- cpu: 1.4 GHz Intel Core i5
- iojs: v2.3.0
- v8: 4.2.77.20
try_catch
with try catch ................................. 35,607 op/s
without try catch .............................. 38,063 op/s
regex_method
String.match ................................... 15,127,762 op/s
Regex.exec ..................................... 15,222,764 op/s
String.search .................................. 14,978,548 op/s
test ........................................... 7,909,194 op/s
random_int
Math.random % range ............................ 96,766,824 op/s
lodash.random .................................. 22,369,157 op/s
is_object_empty
Object.keys().length === 0 ..................... 5,619,592 op/s
lodash.isEmpty(obj) ............................ 2,425,126 op/s
Array.length === 0 ............................. 146,193,232 op/s
lodash.isEmpty(arr) ............................ 36,581,344 op/s
sample_from_array
Math.random % arr.length ....................... 91,554,398 op/s
lodash.sample .................................. 15,749,745 op/s
uniq_str_array
obj[key] = true ................................ 5,957 op/s
lodash.uniq .................................... 11,333 op/s
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
JSON.parse(JSON.stringify) ..................... 192,176 op/s
lodash.cloneDeep .................................... 151,387 op/s
lodash.clone. this is shadow clone .................. 918,130 op/s
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
withoutHiddenClass ............................. 40,051,618 op/s
withHiddenClass ................................ 54,829,686 op/s
inner_function
inner .......................................... 23,634,031 op/s
outter ......................................... 94,173,853 op/s
iterate_object
for .. in .. ................................... 17,082,395 op/s
Object.keys .................................... 862,567 op/s
lodash.forEach ................................. 1,078,065 op/s
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
new Array() .................................... 58,084,959 op/s
[] ............................................. 162,278,430 op/s
next_tick
process.nextTick ............................... 2,315 op/s
setTimeout(0) .................................. 1,938 op/s
setImmediate ................................... 1,349 op/s
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
+ .............................................. 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
+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
- add your test to
benchmark
dir - run
$ make benchmark file=benchmark/xxx.js
to run the benchmark - then send a pr to me, I would add the result to README.md