Skip to content

Instantly share code, notes, and snippets.

@FLamparski
Created August 16, 2017 09:32
Show Gist options
  • Save FLamparski/39957befd1f1501e0ac926ad489025e7 to your computer and use it in GitHub Desktop.
Save FLamparski/39957befd1f1501e0ac926ad489025e7 to your computer and use it in GitHub Desktop.
Delete vs undefined (https://jsbench.github.io/#39957befd1f1501e0ac926ad489025e7) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Delete vs undefined</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 () {
let nRoutes = 1000;
window.routes = new Array(nRoutes);
while (nRoutes--) {
routes[nRoutes] = {
hops: makeHops(),
};
}
function makeHops() {
let nHops = 10;
const hops = new Array(nHops);
while (nHops--) {
hops[nHops] = {
link: {
source: 'some source',
target: 'some target',
metric1: 'metric1',
metric2: 'metric2',
metric3: 'metric3',
metric4: 'metric4',
metric5: 'metric5',
},
};
}
return hops;
}
};
Benchmark.prototype.teardown = function () {
// ensure the compiler doesn't optimise everything out and that our benchmark
// has done the correct thing
console.log(check(routes));
function check(routes) {
return routes.every((route, i) => route.hops.every((hop, j) => hop.link.source === 'some source' && !hop.link[`metric${(i * j + j) % 5 + 1}`]));
}
};
suite.add("for (let i = 0; i < routes.length; i++) {", function () {
for (let i = 0; i < routes.length; i++) {
const route = routes[i];
for (let j = 0; j < route.hops.length; j++) {
const hop = route.hops[j];
delete hop.link.metric1;
delete hop.link.metric2;
delete hop.link.metric3;
delete hop.link.metric4;
delete hop.link.metric5;
}
}
});
suite.add("for (let i = 0; i < routes.length; i++) {", function () {
for (let i = 0; i < routes.length; i++) {
const route = routes[i];
for (let j = 0; j < route.hops.length; j++) {
const hop = route.hops[j];
hop.link.metric1 = hop.link.metric2 = hop.link.metric3 = hop.link.metric4 = hop.link.metric5 = undefined;
}
}
});
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("Delete vs undefined");
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