Skip to content

Instantly share code, notes, and snippets.

@jridgewell
Last active October 2, 2019 17:18
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 jridgewell/4faa08f91d4bc895c1f01c8768ae48d2 to your computer and use it in GitHub Desktop.
Save jridgewell/4faa08f91d4bc895c1f01c8768ae48d2 to your computer and use it in GitHub Desktop.
Weakmap vs Object vs Private State (https://jsbench.github.io/#4faa08f91d4bc895c1f01c8768ae48d2) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Weakmap vs Object vs Private State</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 weakmap = new WeakMap()
const polymap = {
uid: Symbol(),
get(key) { return key[this.uid] },
set(key, value) { return key[this.uid] = value }
}
const privateName = (function PrivateName() {
class Super {
constructor(object) {
return object;
}
}
class State extends Super {
#data;
constructor(object) {
super(object);
}
static init(object, value) {
new State(object);
object.#data = value;
}
static has(object) {
try {
object.#data;
return true;
} catch (e) {
return false;
}
}
static get(object) {
return object.#data;
}
static set(object, value) {
object.#data = value;
}
}
return {
__proto__: null,
has: State.has,
init: State.init,
get: State.get,
set: State.set,
};
})();
const privateSymbol = (function() {
try {
const sym = eval("%CreatePrivateSymbol('[[sym]]')");
return {
get(object) {
return object[sym];
},
set(object, value) {
return object[sym] = value;
},
}
} catch {
// alert('Reload in V8 with --allow-natives-syntax');
throw new Error('Reload in V8 with --allow-natives-syntax');
}
})();
function fn1 () {}
function fn2 () {}
function fn3 () {}
function fn4 () {}
};
suite.add("WeakMap", function () {
// WeakMap
var obj = {}
weakmap.set(obj, 1)
if (!weakmap.get(fn1)) {
weakmap.set(fn1, 2)
} else if (weakmap.get(fn1) !== 2) {
throw 1
}
if (weakmap.get(obj) !== 1) {
throw 1
}
});
suite.add("WeakMap via Symbol", function () {
// WeakMap via Symbol
var obj = {}
polymap.set(obj, 1)
if (!polymap.get(fn2)) {
polymap.set(fn2, 2)
} else if (polymap.get(fn2) !== 2) {
throw 1
}
if (polymap.get(obj) !== 1) {
throw 1
}
});
suite.add("WeakMap via Private Fields", function () {
// WeakMap via Private Fields
var obj = {}
privateName.init(obj, 1)
if (!privateName.has(fn3)) {
privateName.init(fn3, 2)
} else if (privateName.get(fn3) !== 2) {
throw 1
}
if (privateName.get(obj) !== 1) {
throw 1
}
});
suite.add("WeakMap via Private Symbols", function () {
// WeakMap via Private Symbols
var obj = {}
privateSymbol.set(obj, 1)
if (!privateSymbol.get(fn4)) {
privateSymbol.set(fn4, 2)
} else if (privateSymbol.get(fn4) !== 2) {
throw 1
}
if (privateSymbol.get(obj) !== 1) {
throw 1
}
});
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("Weakmap vs Object vs Private State");
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