Skip to content

Instantly share code, notes, and snippets.

@KelWill
Created June 14, 2021 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KelWill/26b41b8cfd441ba72ae513a64971df37 to your computer and use it in GitHub Desktop.
Save KelWill/26b41b8cfd441ba72ae513a64971df37 to your computer and use it in GitHub Desktop.
How much slower is Array.concat compared to Array.push
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