Skip to content

Instantly share code, notes, and snippets.

@philihp
Last active March 23, 2022 18:03
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 philihp/1c821a7f1bd3056c0256e055a60967b1 to your computer and use it in GitHub Desktop.
Save philihp/1c821a7f1bd3056c0256e055a60967b1 to your computer and use it in GitHub Desktop.
Functional Benchmarks
const Benchmark = require("benchmark");
const Suite = new Benchmark.Suite();
const numColumns = 3;
const arr = Array(100).map((_, i) => i);
const testControl = (index) => {
const ret = [];
for (let kk = 0; kk < numColumns; kk++) {
const item = data[index * numColumns + kk];
if (item != null) {
ret.push(item);
}
}
return ret;
};
const testFiltery = (index) => {
return arr
.slice(index * numColumns, (index + 1) * numColumns)
.filter((item) => item != null);
};
Suite.add("testFiltery", () => {
arr.forEach(testFiltery);
})
.add("testControl", () => {
arr.forEach(testControl);
})
.on("cycle", (e) => {
console.log(e.target.toString());
})
.run({ async: true });
$ node index.js
testFiltery x 4,707,602 ops/sec ±0.13% (99 runs sampled)
testControl x 4,707,246 ops/sec ±0.10% (95 runs sampled)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment