Skip to content

Instantly share code, notes, and snippets.

@wulucxy
Created February 8, 2020 03:38
Show Gist options
  • Save wulucxy/8a43ee7d7cbb3a0f05ba9c7fa996d2f9 to your computer and use it in GitHub Desktop.
Save wulucxy/8a43ee7d7cbb3a0f05ba9c7fa996d2f9 to your computer and use it in GitHub Desktop.
#benchmark
const { performance } = require('perf_hooks');
var mapLoop = function(arr, callback){
return arr.map(callback)
}
var forLoop = function(arr, callback){
for(let i=0; i < arr.length; i++){
callback(arr[i], i, arr)
}
}
var run = function(name, times, fn, arr, callback){
var start = performance.now()
for(let i=0; i<times; i++){
fn(arr, callback)
}
var end = performance.now()
console.log('Running %s %d times cost %d ms', name, times, end - start)
}
var testArr = [0, 1, 2, 3, 4, 5, 6]
var callback = d => d
run('mapLoop', 10000000, mapLoop, testArr, callback)
run('forLoop', 10000000, forLoop, testArr, callback)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment