-
-
Save wulucxy/8a43ee7d7cbb3a0f05ba9c7fa996d2f9 to your computer and use it in GitHub Desktop.
#benchmark
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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