Last active
June 27, 2019 07:53
-
-
Save ra100/7b01fd22213c0e0e75de622c778cabeb to your computer and use it in GitHub Desktop.
Perfomance comparison of arrays
This file contains 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
#!/usr/bin/env node | |
const Benchmark = require('benchmark') | |
const spread = (d1, d2) => () => { | |
const d = [...d1, ...d2] | |
} | |
const concat = (d1, d2) => () => { | |
const d = d1.concat(d2) | |
} | |
const scenario = (d1, d2, name, r) => { | |
console.info(name) | |
suite = new Benchmark.Suite() | |
suite | |
.add('spread', spread(d1, d2)) | |
.add('concat', concat(d1, d2)) | |
.on('cycle', event => { | |
console.info(String(event.target)) | |
}) | |
.on('complete', () => { | |
const difference = `${1 / (suite['0'].hz / suite['1'].hz)}`.slice(0, 5) | |
console.info('Fastest is ' + suite.filter('fastest').map('name')) | |
console.info(`Concat is ${difference} times faster`) | |
r.push({ length: name, difference: Number(difference) }) | |
console.info() | |
}) | |
.run() | |
} | |
const createArrays = length => { | |
const data1 = [] | |
const data2 = [] | |
while (data1.length < length) { | |
data1.push(Math.random()) | |
data2.push(Math.random()) | |
} | |
return [data1, data2] | |
} | |
const results = [] | |
for (let i = 1; i <= 524288; i *= 2) { | |
const [data1, data2] = createArrays(i) | |
scenario(data1, data2, i, results) | |
} | |
console.info(JSON.stringify(results)) | |
console.table(results) |
This file contains 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
#!/usr/bin/env node | |
const Benchmark = require('benchmark') | |
const uniq = require('lodash.uniq') | |
const set = d => () => [...new Set(d)] | |
const lodash = d => () => uniq(d) | |
const object = d => () => { | |
const n = {} | |
for (const a of d) { | |
if (!n[a]) { | |
n[a] = true | |
} | |
} | |
return Object.keys(n) | |
} | |
const scenario = (d, name, r) => { | |
console.info(name) | |
suite = new Benchmark.Suite() | |
suite | |
.add('Set', set(d)) | |
.add('uniq', lodash(d)) | |
.add('object', object(d)) | |
.on('cycle', event => { | |
console.info(String(event.target)) | |
}) | |
.on('complete', () => { | |
const difference = `${1 / (suite['0'].hz / suite['1'].hz)}`.slice(0, 5) | |
console.info('Fastest is ' + suite.filter('fastest').map('name')) | |
console.info(`uniq is ${difference} times faster`) | |
r.push({ | |
length: name, | |
set: suite['0'].hz, | |
uniq: suite['1'].hz, | |
object: suite['2'].hz, | |
}) | |
console.info() | |
}) | |
.run() | |
} | |
const createArrays = length => { | |
const data = [] | |
while (data.length < length) { | |
data.push(Math.floor(Math.random() * 100) + 'asdf') | |
} | |
return data | |
} | |
const results = [] | |
for (let i = 1; i <= 524288; i *= 3) { | |
const data = createArrays(i) | |
scenario(data, i, results) | |
} | |
console.info(JSON.stringify(results)) | |
console.table(results) |
This file contains 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
{ | |
"name": "arrays", | |
"version": "1.0.0", | |
"description": "", | |
"main": "arrays-spread-concat.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "WTFPL", | |
"dependencies": { | |
"benchmark": "^2.1.4", | |
"microtime": "^3.0.0" | |
} | |
} |
This file contains 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
#!/usr/bin/env node | |
const Benchmark = require('benchmark') | |
const from = set => () => Array.from(set) | |
const spread = set => () => [...set] | |
const scenario = (d, name, r) => { | |
console.info(name) | |
suite = new Benchmark.Suite() | |
suite | |
.add('from', from(d)) | |
.add('spread', spread(d)) | |
.on('cycle', event => { | |
console.info(String(event.target)) | |
}) | |
.on('complete', () => { | |
const difference = `${1 / (suite['0'].hz / suite['1'].hz)}`.slice(0, 5) | |
console.info('Fastest is ' + suite.filter('fastest').map('name')) | |
console.info(`spread is ${difference} times faster`) | |
r.push({ | |
length: name, | |
from: suite['0'].hz, | |
spread: suite['1'].hz, | |
}) | |
console.info() | |
}) | |
.run() | |
} | |
const createArrays = length => { | |
const data = new Set() | |
let i = 0 | |
while (i < length) { | |
i++; | |
data.add(i) | |
} | |
return data | |
} | |
const results = [] | |
for (let i = 1; i <= 524288; i *= 3) { | |
const data = createArrays(i) | |
scenario(data, i, results) | |
} | |
console.info(JSON.stringify(results)) | |
console.table(results) |
This file contains 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
#!/usr/bin/env node | |
console.table([ | |
{ length: 1, difference: '0.400' }, | |
{ length: 2, difference: '0.463' }, | |
{ length: 4, difference: '0.583' }, | |
{ length: 8, difference: '0.825' }, | |
{ length: 16, difference: '1.391' }, | |
{ length: 32, difference: '2.296' }, | |
{ length: 64, difference: '4.569' }, | |
{ length: 128, difference: '5.006' }, | |
{ length: 256, difference: '5.730' }, | |
{ length: 512, difference: '5.923' }, | |
{ length: 1024, difference: '5.738' }, | |
{ length: 2048, difference: '5.843' }, | |
{ length: 4096, difference: '5.973' }, | |
{ length: 8192, difference: '5.680' }, | |
{ length: 16384, difference: '2.517' }, | |
{ length: 32768, difference: '3.633' }, | |
{ length: 65536, difference: '3.661' }, | |
{ length: 131072, difference: '3.656' }, | |
{ length: 262144, difference: '3.495' }, | |
{ length: 524288, difference: '3.686' }, | |
]) | |
const uniq = [ | |
{ | |
length: 1, | |
set: 14914502.072059998, | |
uniq: 28977719.561452013, | |
object: 43069120.22290404, | |
}, | |
{ | |
length: 3, | |
set: 9287553.632566154, | |
uniq: 14209676.712241232, | |
object: 6791106.836578766, | |
}, | |
{ | |
length: 9, | |
set: 1725209.9584135, | |
uniq: 2797466.077989172, | |
object: 2295201.40095648, | |
}, | |
{ | |
length: 27, | |
set: 819131.6186856466, | |
uniq: 420905.3776180805, | |
object: 251675.42099713307, | |
}, | |
{ | |
length: 81, | |
set: 311508.18579122034, | |
uniq: 60675.43720847883, | |
object: 124050.75618034205, | |
}, | |
{ | |
length: 243, | |
set: 112353.48986365563, | |
uniq: 97557.40979953301, | |
object: 70737.9398952817, | |
}, | |
{ | |
length: 729, | |
set: 39371.10094001782, | |
uniq: 37104.89379856549, | |
object: 45626.25180213333, | |
}, | |
{ | |
length: 2187, | |
set: 13103.453164126297, | |
uniq: 12852.926388233054, | |
object: 23923.754300316305, | |
}, | |
{ | |
length: 6561, | |
set: 4396.136184077777, | |
uniq: 4369.279517597667, | |
object: 9823.527194692877, | |
}, | |
{ | |
length: 19683, | |
set: 1499.3383051910262, | |
uniq: 1490.4432679602915, | |
object: 3563.9390048257665, | |
}, | |
{ | |
length: 59049, | |
set: 498.4733212577199, | |
uniq: 498.25938333652334, | |
object: 1227.4377349232964, | |
}, | |
{ | |
length: 177147, | |
set: 162.23650477140012, | |
uniq: 160.15877908480127, | |
object: 393.2691014842595, | |
}, | |
] | |
uniq.forEach(c => { | |
console.info(c.length, c.set, c.uniq, c.object) | |
}) | |
const spread = [ | |
{ length: 1, from: 44311680.7042605, spread: 51333826.11007402 }, | |
{ length: 3, from: 35166063.727507085, spread: 40301614.10772243 }, | |
{ length: 9, from: 23501329.84959788, spread: 25603594.892988913 }, | |
{ length: 27, from: 10961935.033519723, spread: 11336429.1350863 }, | |
{ length: 81, from: 4648454.693698098, spread: 4798306.393412219 }, | |
{ length: 243, from: 1535427.8937938057, spread: 1524862.4038308416 }, | |
{ length: 729, from: 531567.6010926277, spread: 534604.4981999898 }, | |
{ length: 2187, from: 180543.69722151992, spread: 181837.7147992214 }, | |
{ length: 6561, from: 60161.45692739542, spread: 60299.741561676965 }, | |
{ length: 19683, from: 20172.73978510829, spread: 20106.096972295887 }, | |
{ length: 59049, from: 3507.5073456032865, spread: 3502.089956543653 }, | |
{ length: 177147, from: 1140.717456577785, spread: 1149.4110362037086 }, | |
] | |
spread.forEach(c => { | |
console.info(c.length, c.from, c.spread) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment