Created
June 14, 2021 16:25
-
-
Save KelWill/26b41b8cfd441ba72ae513a64971df37 to your computer and use it in GitHub Desktop.
How much slower is Array.concat compared to Array.push
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"); | |
const _ = require("lodash"); | |
const suite = new Benchmark.Suite(); | |
const TIMES = 10000; | |
suite | |
.add("pushing", function () { | |
const docs = []; | |
for (let i = 0; i < TIMES; i++) { | |
docs.push(new Date()); | |
} | |
}) | |
.add("concat", function () { | |
let docs = []; | |
for (let i = 0; i < TIMES; i++) { | |
docs = docs.concat(new Date()); | |
} | |
}) | |
.on("cycle", function (event) { | |
console.log(String(event.target)); | |
}) | |
.on("complete", function () { | |
console.log("Fastest is " + this.filter("fastest").map("name")); | |
}) | |
// run async | |
.run({ async: true }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment