Created
October 2, 2019 18:22
-
-
Save jridgewell/df58ced0052eb86792b2e996615ac5cc to your computer and use it in GitHub Desktop.
Directive Implementations: WeakMap vs Property vs Private Symbol (https://jsbench.github.io/#df58ced0052eb86792b2e996615ac5cc) #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 vs Private Symbol</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 () { | |
let i = 0; | |
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 directiveProperty(f) { | |
return (...args) => { | |
const d = f(...args); | |
d.__isDirective = true; | |
return d; | |
}; | |
} | |
const adderDirectiveProperty = directiveProperty(x => { | |
return y => x + y; | |
}); | |
let sym; | |
try { | |
sym = eval("%CreatePrivateSymbol('directive')"); | |
} catch { | |
throw new Error('reload v8 with --allow-natives-syntax'); | |
} | |
function directivePrivateSymbol(f) { | |
return (...args) => { | |
const d = f(...args); | |
d[sym] = true; | |
return d; | |
}; | |
} | |
const adderDirectivePrivateSymbol = directivePrivateSymbol(x => { | |
return y => 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 = adderDirectiveProperty(1); | |
if (add.__isDirective) { | |
i += add(2); | |
} | |
}); | |
suite.add("Using Private Symbol", function () { | |
// Using Private Symbol | |
i = 0; | |
const add = adderDirectivePrivateSymbol(1); | |
if (add[sym]) { | |
i += add(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 vs Private Symbol"); | |
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