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
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment