Skip to content

Instantly share code, notes, and snippets.

@rektide
Last active August 23, 2022 18:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rektide/bdd6c20c07e440f32d567c59ad4f5e9a to your computer and use it in GitHub Desktop.
Save rektide/bdd6c20c07e440f32d567c59ad4f5e9a to your computer and use it in GitHub Desktop.
Unique-ify: In Place or At Finish (http://jsbench.github.io/#bdd6c20c07e440f32d567c59ad4f5e9a) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Unique-ify: In Place or At Finish</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2>
</body>
</html>
"use strict";
(function (factory) {
if (typeof Benchmark !== "undefined") {
factory(Benchmark);
} else {
factory(require("benchmark"));
}
})(function (Benchmark) {
var suite = new Benchmark.Suite;
Benchmark.prototype.setup = function () {
const MAX_NUMBER = 10000;
const BATCH_SIZE = 500;
const BATCHES = 4000;
function randomNumber() {
return Math.floor(Math.random() * MAX_NUMBER);
}
// create an array with `n` number of `entry`'s in it'
function batch(n = BATCH_SIZE, entry = randomNumber) {
const b = new Array(n)
for (let i = 0 ; i < BATCH_SIZE; ++i) {
b[i] = entry()
}
return b
}
// create an array of many batches
function batches() {
const bb = batch(BATCHES, batch)
return bb
}
let BB = batches()
};
Benchmark.prototype.teardown = function () {
BB = null
};
suite.add("function inPlace() {", function () {
function inPlace() {
const s = new Set()
for (let bb in BB) {
const B = BB[bb]
for (let b in B) {
s.add(B[b])
}
}
return Array.from(s)
}
inPlace()
});
suite.add("function atEndApply() {", function () {
function atEndApply() {
const arr = new Array()
for (let bb in BB) {
const B = BB[bb]
arr.push.apply(arr, B) // add each batch into array
}
const s = new Set(arr)
return Array.from(s)
}
atEndApply()
});
suite.add("NO DEDUPLICATION - JUST FOR REFERENCE", function () {
// NO DEDUPLICATION - JUST FOR REFERENCE
function noDedupeApply() {
const arr = new Array()
for (let bb in BB) {
const B = BB[bb]
arr.push.apply(arr, B)
}
return arr
}
noDedupeApply()
});
suite.add("function atEndCall() {", function () {
function atEndCall() {
const arr = new Array()
for (let bb in BB) {
const B = BB[bb]
arr.push.call(arr, ...B)
}
const s = new Set(arr)
return Array.from(s)
}
atEndCall()
});
suite.add("NO DEDUPLICATION - JUST FOR REFERENCE", function () {
// NO DEDUPLICATION - JUST FOR REFERENCE
function noDedupeCall() {
const arr = new Array()
for (let bb in BB) {
const B = BB[bb]
arr.push.call(arr, ...B)
}
return arr
}
noDedupeCall()
});
suite.add("function atEndApplySpread() {", function () {
function atEndApplySpread() {
const arr = new Array()
for (let bb in BB) {
const B = BB[bb]
arr.push(...B) // use spread, instead of .push.apply
}
const s = new Set(arr)
return Array.from(s)
}
atEndApplySpread()
});
suite.on("cycle", function (evt) {
console.log(" - " + evt.target);
});
suite.on("complete", function (evt) {
console.log(new Array(30).join("-"));
var results = evt.currentTarget.sort(function (a, b) {
return b.hz - a.hz;
});
results.forEach(function (item) {
console.log((idx + 1) + ". " + item);
});
});
console.log("Unique-ify: In Place or At Finish");
console.log(new Array(30).join("-"));
suite.run();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment