35 lines
1.1 KiB
JavaScript
35 lines
1.1 KiB
JavaScript
const x = [];
|
|
for (let i = 0; i < 50_000_000; i++) {
|
|
x[i] = Math.random();
|
|
}
|
|
|
|
const for_test_list = [...x]
|
|
const for_timings = []
|
|
for (let i = 0; i < 10; i++) {
|
|
const startTime = performance.now()
|
|
for (let i = 0; i < for_test_list.length; i++) {
|
|
for_test_list[i] * 2
|
|
}
|
|
const timeElapsed = performance.now() - startTime
|
|
for_timings.push(timeElapsed)
|
|
console.log(`for#${i + 1} time: ${timeElapsed.toFixed(4)} ms`)
|
|
}
|
|
console.log(`Average 'for' time: ${(for_timings.reduce((a,b) => a+b)/for_timings.length).toFixed(4)} ms`)
|
|
print_xvals(for_test_list)
|
|
|
|
const map_test_list = [...x]
|
|
const map_timings = []
|
|
for (let i = 0; i < 10; i++) {
|
|
const startTime = performance.now()
|
|
map_test_list.map(val => val*2)
|
|
const timeElapsed = performance.now() - startTime
|
|
map_timings.push(timeElapsed)
|
|
console.log(`map#${i + 1} time: ${timeElapsed.toFixed(4)}`)
|
|
}
|
|
console.log(`Average 'map' time: ${(map_timings.reduce((a,b) => a+b)/for_timings.length).toFixed(4)} ms`)
|
|
print_xvals(map_test_list)
|
|
|
|
function print_xvals(x) {
|
|
console.log(x[0], x[Math.floor(x.length / 2)], x[x.length - 1])
|
|
}
|