Last active
November 15, 2020 17:22
-
-
Save rangeoshun/34527333f499ded69aed181c0396f6f4 to your computer and use it in GitHub Desktop.
JS optimization 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"); | |
const asciichart = require("asciichart"); | |
const sort = (arr) => arr.slice().sort((a, b) => a - b); | |
const median = (arr) => { | |
const mid = Math.floor(arr.length / 2), | |
nums = sort(arr); | |
return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2; | |
}; | |
const p99 = (arr) => sort(arr)[Math.floor((arr.length / 100) * 99)]; | |
const min = (arr) => sort(arr)[0]; | |
const max = (arr) => sort(arr)[arr.length - 1]; | |
const first = (arr) => arr[0]; | |
const randArray = () => Array(1000).fill(null).map(Math.random); | |
const sum = (acc, num) => acc + num; | |
const freducer = (acc, num, ind, nums, rest, restSum) => ( | |
(rest = nums.slice(ind)), (restSum = rest.reduce(sum)), acc + restSum | |
); | |
const creducer = (acc, num, ind, nums) => { | |
const rest = nums.slice(ind); | |
const restSum = rest.reduce(sum); | |
return acc + restSum; | |
}; | |
const sreducer = (acc, num, ind, nums) => acc + nums.slice(ind).reduce(sum); | |
const n = 200; | |
const pit = []; | |
const freducerTimes = []; | |
for (let i = 0; i < n; i++) { | |
const array = randArray(); | |
let start = performance.now(); | |
pit.push(array.reduce(freducer, 0)); | |
let end = performance.now(); | |
freducerTimes.push(end - start); | |
} | |
const creducerTimes = []; | |
for (let i = 0; i < n; i++) { | |
const array = randArray(); | |
let start = performance.now(); | |
pit.push(array.reduce(creducer, 0)); | |
let end = performance.now(); | |
creducerTimes.push(end - start); | |
} | |
const sreducerTimes = []; | |
for (let i = 0; i < n; i++) { | |
const array = randArray(); | |
let start = performance.now(); | |
pit.push(array.reduce(sreducer, 0)); | |
let end = performance.now(); | |
sreducerTimes.push(end - start); | |
} | |
console.log(pit.reduce(sum)); | |
console.log(); | |
console.log(asciichart.plot(freducerTimes)); | |
console.log(`freducer avg: ${freducerTimes.reduce(sum) / n}ms`); | |
console.log(` med: ${median(freducerTimes)}ms`); | |
console.log(` min: ${min(freducerTimes)}ms`); | |
console.log(` max: ${max(freducerTimes)}ms`); | |
console.log(` 1st: ${first(freducerTimes)}ms`); | |
console.log(` p99: ${p99(freducerTimes)}ms`); | |
console.log(` tot: ${freducerTimes.reduce(sum)}ms`); | |
console.log(); | |
console.log(asciichart.plot(creducerTimes)); | |
console.log(`creducer avg: ${creducerTimes.reduce(sum) / n}ms`); | |
console.log(` med: ${median(creducerTimes)}ms`); | |
console.log(` min: ${min(creducerTimes)}ms`); | |
console.log(` max: ${max(creducerTimes)}ms`); | |
console.log(` 1st: ${first(creducerTimes)}ms`); | |
console.log(` p99: ${p99(creducerTimes)}ms`); | |
console.log(` tot: ${creducerTimes.reduce(sum)}ms`); | |
console.log(); | |
console.log(asciichart.plot(sreducerTimes)); | |
console.log(`sreducer avg: ${sreducerTimes.reduce(sum) / n}ms`); | |
console.log(` med: ${median(sreducerTimes)}ms`); | |
console.log(` min: ${min(sreducerTimes)}ms`); | |
console.log(` max: ${max(sreducerTimes)}ms`); | |
console.log(` 1st: ${first(sreducerTimes)}ms`); | |
console.log(` p99: ${p99(sreducerTimes)}ms`); | |
console.log(` tot: ${sreducerTimes.reduce(sum)}ms`); |
Author
rangeoshun
commented
Nov 15, 2020
•
200 runs with arrays of 10000 random numbers with Node v15.2.0.
1106.78 ┼╮
1105.78 ┤│
1104.78 ┤│
1103.78 ┤│
__________________
exluded
__________________
119.64 ┤│
118.64 ┤│
117.64 ┤│
116.64 ┤│
115.64 ┤│
114.64 ┤│
113.64 ┤│
112.64 ┤│
111.64 ┤│ ╭╮ ╭╮ ╭╮
110.63 ┤│ ││ ││ ╭╮ ╭╮ ││
109.63 ┤│ ││ ││ ╭╮ ││ ││ ││
108.63 ┤│ ││ ││ ││ ││ ││ ││
107.63 ┤│ ││ ││ ││ ││ ││ ││
106.63 ┤│ ││ ││ ││ │╰╮ ││ ││
105.63 ┤│ ││ ││ ││ │ │ ╭╮ ╭╮ ││ ││
104.63 ┤│ │╰╮ ││ │╰╮ │ ╰╮ ││ ││ ╭╮ ││ ╭╮ ╭╯│
103.63 ┤│ ╭╯ ╰─╮│╰╮ │ │╭─╯ │ ││ ││ ╭╮ ││ ││ ││ │ │ ╭╮
102.63 ┤│ │ ╰╯ │ │ ╰╯ │ ╭╮ ││ ││ ││ ││ ╭╮ ╭╮ ││ ││ ╭╮ ╭╮ │ │ ││ ╭╮ ╭╮ ╭╮
101.63 ┤│ │ │ │ │ ╭╮ ││ ╭╮ ││ ╭╮ ││ ╭╮ ││ ││ ╭╮ ││ ╭╮ ││ ││ ││ ││ ││ │ │ ││ ││ ╭╮ ╭╮ ││ ││
100.63 ┤│╭╮│ │ ╭╯ │ ││ ││ ││ ││ ││ │╰╮ ╭╮ ││ ││ ││ ╭╮ ││ ││ ╭╮││ ││ │╰╮ ││ ││ │╰╮ │ │ ││ ╭╮ ││ ││ │╰╮ ││ ││
99.63 ┤╰╯││ ╰╮│ │ ││ ││╭╮ ││ ││ ││ │ │ ││ ││ ││ ╭╮ ││ ╭─╮││ ││ ╭╮ ││ ││││ ││╭╮ │ │ ││ ││ │ │ │ │ ││ ││ ╭╯│ ╭╮ ││ ╭╮ │ │ ││ ││ ╭╮
98.63 ┤ ││ ││ │ ││ ││││ ││ │╰╮ ││ │ │ ││ ││ ││ ╭╮ ││ │╰╮│ │││ ││ ││ │╰╮││││ ││││ │ │ ││ ││ │ │ │ │ │╰╮ ││ │ │ ││ ││ │╰╮ │ │ │╰╮ ││ ││
97.63 ┤ ╰╯ ╰╯ │ │╰╮ ││││ ││ │ │ ││ │ │ │╰╮╭╮ │╰╮ │╰╮││ ││ │ ││ ╰╯│ │╰╮╭╮╭╯│ ╭─╯ ╰╯│││ │╰╯│ │ │ │╰╮ ││ │ │ │ ╰╮ │ │ ││ │ ╰╮ ││ ╭╯│ │ │ │ ╰╮ │ │ ││╭╮ ││
96.63 ┤ │ │ ╰─╮│╰╯│ ││ ╭╯ │ │╰╮ │ ╰─╮│ ╰╯│ │ │ ╭╯ ││╰╮││ ╭╮│ ││ │ │ ││╰╯ │╭╯ ││╰╮╭╮ │ │ │ │ ╭╯ ╰─╮│╰╮ │ ╰╮╭─╯ │ │ │ ││╭╮│ │╭╮ ││ │ ╰╮╭─╯ │ │ ╰╮│ │╭╮╭╯╰╯│╭╯╰╮
95.63 ┤ │ │ ╰╯ ╰─╯│ ╭╯ ╰──╯ │ │ ╰╯ │ │ ╰─╯ ╰╯ │││ │╰╯ ╰╯ │ │ ╰╯ ││ ╰╯ ╰╯╰─╯ ╰─╯ │ │ ╰╯ │ │ ╰╯ │╭╯ ╰──╮│╰╯╰╯ ╰╯╰─╯│ ╭╯ ╰╯ │ │ ╰╯ ╰╯││ ╰╯ │╭──
94.63 ┤ │ │ │ │ │ │ │ │ ╰╯│╭╯ │ ╭╯ ││ │╭─╯ │ │ ││ ╰╯ ╰─╯ │ │ ╰╯ ╰╯
93.63 ┤ │ │ ╰╮ │ │ │ │ │ ╰╯ ╰─╮│ ╰╯ ╰╯ ╰─╮│ ╰╯ ╰╮ │
92.63 ┤ ╰──╯ ╰─╯ ╰───╯ ╰─╯ ╰╯ ╰╯ ╰──╯
freducer avg: 103.40833139955997ms
med: 97.28297898173332ms
min: 92.63221698999405ms
max: 1106.780827999115ms
1st: 1106.780827999115ms
p99: 112.3241840004921ms
tot: 20681.666279911995ms
1020.40 ┼╮
1019.40 ┤│
1018.40 ┤│
__________________
exluded
__________________
120.71 ┤│
119.71 ┤│
118.71 ┤│
117.71 ┤│
116.70 ┤│
115.70 ┤│
114.70 ┤│
113.70 ┤│
112.70 ┤│
111.70 ┤│
110.70 ┤│
109.70 ┤│
108.70 ┤│
107.70 ┤│ ╭╮ ╭╮
106.70 ┤│ ││ ╭╮ ││
105.70 ┤│ ││ ││ ╭╮ ││
104.70 ┤│ ╭╮ ││ ╭╮ ││ ││ ╭╮ ││
103.69 ┤│ ╭╮ ││ ││ ││ ││ ││ ╭╮ ╭╮ ╭╮ ╭╮ ││ ╭╮ ╭╮ ╭╮ ││
102.69 ┤│ ││ ╭╮ ││ ╭╮ │╰╮ ││ ││╭╮ ││ ╭╮ ││ ││ ││ ╭╮ ││ ╭╮ ││ ││ ╭╮││ ││ ╭╮ ││
101.69 ┤╰╮ ││ ╭╮ ││ ││ ╭╮ ││ │ │ ╭╮ ╭╮ ││ ││││ ││ ╭╮ ││ ││ ││ ╭╮ ││ ╭╮ ││ ││ ││ ││ ││ ││││ ││ ╭╯│ ││
100.69 ┤ │ ╭╮ ││ ││ ││ ││ ││ ││ │ │ ╭╮ ││ ││ ││ ╭╮ ││││ ││ ││ ││ ╭╮ ││ ││ ││ ││ ││ ││ ││ ╭╮ ││ ││ ││ ││││ ││ │ │ ││ ╭╮
99.69 ┤ │ ││ ││ ││ ││ ╭╮ ││ ╭╮ ││ ││ │ │ ││ ││ ││ ││ ││ ││││ ╭╮ ││ ││ ╭╯│ ││ ││ ││ ││ ││ ││ ││ ││ ││ ││ ││ ││ ││││ ││ │ │ ││ ││
98.69 ┤ │ ││ │╰╮ ╭╯│ ││ ││ ││ ││ ││ ││ │ │ ││ ││ ││ ╭╮ ││ ││ ││││ ││ ││ ││ │ │ ││ ││ ││ │╰╮ ╭╯│ │╰─╮ ││ ││ ││ ││ ││ ╭╯│ ││││ │╰╮ │ │ ││ ││
97.69 ┤ ╰╮ ││ ╭╮│ │ │ │ │╰╮ ││ ╭╯│ ││ ││ ││ │ │ │╰╮ ││ │╰─╯│╭╯│ │╰╮ ││││ ││ ╭╯│ ││ │ │ ││ ││ ╭─╯│ │ ╰╮ │ │ │ ╰╮ ││ │╰╮ ││ │╰╮ ││ │ │ │││╰╮ │ │╭╯ ╰╮││ ││
96.69 ┤ │ ││ │╰╯ │╭╮│ │ ╭╯ │╭╮││╭╮╭╯ │ │╰─╮ │╰╮ │╰╮ │ │ │ │╭╮ ││ │ ╰╯ ╰╮ │ ╰╮ │╰╯╰╮││ ╭╯ │ │╰╮ ╭╯ │╭──╯│ │╰─╯ │ │ │╭╯ │ │ │ ││ │ │╭─╮││ │ │ ╭╯│ │ ╰─╯││ │ ╭╯ ╰╯ ││╰─╯│
95.69 ┤ ╰───╯│╭╯ ╰╯╰╯ │ ╭╯ ╰╯││╰╯╰╯ ╰╮ │ ╰──╯ │ │ ╰──╯ │╭╮│ ╰╯╰─╯│ │ │ │ ╰──╯ ││╰──╯ ╰───╯ │ │ ╰╯ │ ╭╮│ ╰╮ │ ╰╯ │ ╭╯ ╰─╯│ ╭╯ ╰╯ │││╭──╯ ╰──╯ │ │ ╰╯ ╰╮╭╯ ╰╯ │
94.69 ┤ ╰╯ │ │ ╰╯ ╰╮ │ │ │ ╰╯││ │ ╭╮│ ╰╮ │ ││ │ │ │ ╭╯││ │ ╭╯ │ ╭╯ ╰╮ │ ╰╯││ ╰╮╭╯ ╰╯ │
93.69 ┤ ╰╮│ │ │ │ ╭╯ ╰╯ ╰─╯╰╯ │ │ ││ │ │ ╰╮│ ╰╯ │╭╯ │╭╯ ╰╮│ ╰╯ ││ ╰
92.69 ┤ ╰╯ ╰─╯ ╰──╯ ╰─╯ ╰╯ ╰─╯ ╰╯ ╰╯ ╰╯ ╰╯ ╰╯
creducer avg: 102.13022407978774ms
med: 96.60743549466133ms
min: 92.68632501363754ms
max: 1020.4010280370712ms
1st: 1020.4010280370712ms
p99: 108.2951290011406ms
tot: 20426.044815957546ms
1052.77 ┼╮
1051.77 ┤│
1050.77 ┤│
1049.77 ┤│
__________________
exluded
__________________
155.04 ┤│
154.04 ┤│
153.04 ┤│
152.04 ┤│
151.04 ┤│
150.05 ┤│ ╭╮
149.05 ┤│ ││
148.05 ┤│ ││
147.05 ┤│ ││
146.05 ┤│ ││
145.05 ┤│ ││
144.05 ┤│ ││
143.05 ┤│ ││
142.05 ┤│ ││
141.05 ┤│ ││
140.05 ┤│ ││
139.05 ┤│ ││
138.05 ┤│ ││
137.05 ┤│ ││
136.05 ┤│ ││
135.05 ┤│ ││
134.05 ┤│ ││
133.05 ┤│ ││ ╭╮
132.05 ┤│ ││ ││
131.05 ┤│ ││ ││
130.05 ┤│ ││ ││
129.05 ┤│ ││ ││
128.05 ┤│ ││ ││
127.05 ┤│ ││ ││
126.05 ┤│ ││ ││
125.05 ┤│ ││ ││
124.05 ┤│ ││ ││
123.05 ┤│ ││ ││
122.05 ┤│ ││ ││
121.05 ┤│ ││ ││
120.05 ┤│ ││ ││
119.05 ┤│ ││ ││
118.06 ┤│ ││ ││
117.06 ┤│ ││ ││
116.06 ┤│ ││ ││
115.06 ┤│ ││ ││
114.06 ┤│ ││ ││
113.06 ┤│ ││ ││
112.06 ┤│ ││ ││
111.06 ┤│ ││ ╭╮ ││
110.06 ┤│ ││ ││ ││
109.06 ┤│ ││ ││ ││
108.06 ┤│ ││ │╰╮││
107.06 ┤│ ││ │ │││
106.06 ┤│ ╭╮ ││ ╭╮ │ │││
105.06 ┤│ ╭╮ ││ ││ ╭╮ ││ ╭╮ ╭╮ │ │││
104.06 ┤│ ││ ││ ╭╮ ╭╮ ╭╮ ││╭╮ ││ ││ ││ ││ │ │││╭╮
103.06 ┤│ ╭╮││ ╭╮ ╭╮ ╭╮ ││ ││ ╭╮ ││ ╭╮ ││ ╭╮││││ ││ ││ ││ ││ ╭╮ ╭╮ │ │││││
102.06 ┤│ ││││ ││ ││ ││ ╭╮ ││ ╭╮ ╭╮ ││ ││ ╭╮ ││ ││ ││ ╭╮ ││││││ ╭╮ ││ ││ ││ ││ ╭╮ ╭╮ ││ ││ │ │││││ ╭╮ ╭
101.06 ┤│ ││││ ││ ││ ╭╮ ││ ││ ││ ││ ││ ││ ││ ││ ││ ││ ││ ││ ││││││ ││ ││ ││ ││ ╭╮ ││ ││ ││ ││ ╭╮ ││ │ ╰╯│││ ││ │
100.06 ┤│ │╰╯│ ││ ││ ││ ││ ││ ││ ││ ╭╮ ││ ││ ││ ││ ││ ││ ││╭╮ ││ ││││││ ╭╮ ││ ││ ││ ││ ││ ││ ││ ││ ││ ││ ││ │ ╰╯│ ││ │
99.06 ┤│ │ ╰╮│╰╮ ╭╮ ││╭╮ ││ ╭╮││ ││ ││ ││ ││ │╰╮ ││ ││ ││ ││ ││ │╰╯│ ││ ││││││ ││ ││ ││ ││ │╰╮ ││ ││ ││ ││ │╰╮ ││ ││ ╭╮│ │ ││ │
98.06 ┤│ │ ╰╯ ╰╮ ││ ││││ ││ ││││╭╮ ││ ││ ││ ││ ╭╮│ │ ││ ╭╮ ││ ╭╯│ ││ ╭╮│╰╮ │ │ │╰╮ │╰╯││╰╮ ││ │╰╮ ││ ╭╮ ╭╮││ ╭╯ │ ││ │╰╮ ││ │╰╮ │ │ │╰╮╭╮ ││╭╯││ ╰─╮ ││╭╮ │
97.06 ┤│ │ │ ││ ││││ ││ │││╰╯│ ││ ││ │╰╮ ││ ╭╯││ │ ╭╯│ │╰╮│╰╮╭╯ │ ╭╮│╰╮│││ │ │ │ │ │ │ ╰╯ │ │╰╮ │ │ ╭╯╰╮ ││ │╰╯│╭─╯ │╭╮ ││ ╭╮│ │ ╭╮│╰╮ │ │ ╭╮│ ╰╮ │ │││╭╯╰╯ ││ │ │╰╯╰╮│
96.06 ┤│╭─╯ │ ││ ╭╮╭╯╰╯│ ││ ╭╯╰╯ ╰╮│╰╮ ╭╯╰───╯ ╰──╯│ │ ╰╯ ╰─╯ │ │ ╰╯ ╰╯ ╰╮ │╰╯ ╰╯││ │ ╭╯ ╰─╯ ╰╮ │ ╰─╮ │ ╰──╯ ╰──╯ │╭─╯│ │ ╰╯ ╰╯│╭╯│ │╰╯ ╰─╯╰╯ │ │ ╰─╯╰╯ │ │ ╰╯╰╯ ││ ╰╮│ ╰╯
95.06 ┤╰╯ ╰─╮││ │╰╯ │ ││ │ ╰╯ │ ╭╯ ╰╮│ ╰╮│ │ │ ╰╯ │ │ │ ╭╯ │ │ ╰╯ ╰╮ │ ││ │╭╯ │ ╭╯ │ │ ││ ╰╯
94.06 ┤ ││╰─╯ ╰╮│╰╮│ │╭╯ ╰╯ ╰╯ ╰─╯ │ ╭╯ │ │ ╰╮│ │ │ ╰╯ ╰╯ ╰╮│ ╰╮│ ╰╯
93.06 ┤ ││ ╰╯ ││ ╰╯ ╰─╯ ╰─╯ ╰╯ ╰─╯ ╰╯ ╰╯
92.06 ┤ ╰╯ ╰╯
sreducer avg: 102.72248899906873ms
med: 96.70575949549675ms
min: 92.06311702728271ms
max: 1052.7654490470886ms
1st: 1052.7654490470886ms
p99: 150.16584903001785ms
tot: 20544.497799813747ms
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment