Created
November 9, 2019 21:46
-
-
Save peterforgacs/c2f35dda50ccfaaa2d93ba2415ec4cf4 to your computer and use it in GitHub Desktop.
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
const { Benchmark } = require("benchmark"); | |
function generateRandomNumberBetween(min, max){ | |
return Math.floor(Math.random() * max) + min; | |
} | |
function generateTupleArray(length) { | |
const tupleArray = []; | |
for (let i = 0; i < length; i++) { | |
tupleArray.push([generateRandomNumberBetween(1, 1e3), generateRandomNumberBetween(1, 1e3)]); | |
} | |
return tupleArray; | |
} | |
// Contains 100 arrays containing elements between 1 and 1000 | |
const arrays = []; | |
for (let i = 0; i < 100; i++) { | |
arrays.push(generateTupleArray(generateRandomNumberBetween(1, 1e3))) | |
} | |
const suite = new Benchmark.Suite(); | |
suite.add("acc.concat(curr)", function() { | |
for (const arr of arrays) { | |
arr.reduce((acc, curr) => acc.concat(curr), []); | |
} | |
}); | |
suite.add("[...acc, ...curr]", function () { | |
for (const arr of arrays) { | |
arr.reduce((acc, curr) => [...acc, ...curr], []); | |
} | |
}); | |
suite.add("[].concat(...data)", function() { | |
for (const arr of arrays) { | |
[].concat(...arr); | |
} | |
}); | |
suite.on("cycle", function(event) { | |
console.log(String(event.target)); | |
}); | |
suite.on("complete", function() { | |
console.log("Fastest is " + this.filter("fastest").map("name")); | |
}) | |
suite.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment