-
-
Save jridgewell/18fc6d1eb87a9e3edbd7295edf9b8f35 to your computer and use it in GitHub Desktop.
Directive Implementations: WeakMap vs Property (https://jsbench.github.io/#18fc6d1eb87a9e3edbd7295edf9b8f35) #jsbench #jsperf
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>Directive Implementations: WeakMap vs Property</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> |
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
"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 directives = new WeakMap(); | |
function directive(f) { | |
return (...args) => { | |
const d = f(...args); | |
directives.set(d, true); | |
return d; | |
}; | |
} | |
const adderDirective = directive(x => { | |
return y => x + y; | |
}); | |
function directive2(f) { | |
return (...args) => { | |
const d = f(...args); | |
d.__isDirective = true; | |
return d; | |
}; | |
} | |
const adderDirective2 = directive2(x => { | |
return y => x + y; | |
}); | |
let i = 0; | |
function opaque(f, inner) { | |
return (...args) => { | |
return {inner, value: f(...args), __isDirective: true}; | |
}; | |
} | |
const adderOpaque = opaque((x) => { | |
return x; | |
}, (x, y) => { | |
return x + y; | |
}); | |
}; | |
suite.add("Using WeakMap", function () { | |
// Using WeakMap | |
i = 0; | |
const add = adderDirective(1); | |
if (directives.has(add)) { | |
i += add(2); | |
} | |
}); | |
suite.add("Using property", function () { | |
// Using property | |
i = 0; | |
const add = adderDirective2(1); | |
if (add.__isDirective) { | |
i += add(2); | |
} | |
}); | |
suite.add("Without closures", function () { | |
// Without closures | |
i = 0; | |
const add = adderOpaque(1); | |
if (add.__isDirective) { | |
const inner = add.inner; | |
i += inner(add.value, 2); | |
} | |
}); | |
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("Directive Implementations: WeakMap vs Property"); | |
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