static vs. dynamic property access (v2)

Revision 2 of this benchmark created on


Description

Various approaches that are relevant to designing a library API where you want to allow the consumer to specify custom keys of objects to operate on

Setup

let object = { a: 42, b: "foo", c: true, d: ["bar"] };

Test runner

Ready to run.

Testing in
TestOps/sec
static key
let key = ["a", "b", "c", "d"][Math.floor(Math.random() * 3)]; // unused, to balance tests
let value = object.a
ready
dynamic-static key
let key = ["a", "b", "c", "d"][Math.floor(Math.random() * 3)]; // unused, to balance tests
let value = object["a"]
ready
dynamic (but fixed) key
let key = "a";
let key_ = ["a", "b", "c", "d"][Math.floor(Math.random() * 3)]; // unused, to balance tests
let value = object[key];
ready
dynamic (and changing) key
let key = ["a", "b", "c", "d"][Math.floor(Math.random() * 3)];
let value = object[key];
ready
dynamic with callback (fixed)
let key_ = ["a", "b", "c", "d"][Math.floor(Math.random() * 3)]; // unused, to balance tests
let getKey = () => "a";
let value = object[getKey()];
ready
dynamic with callback (changing)
let getKey = () => ["a", "b", "c", "d"][Math.floor(Math.random() * 3)];
let value = object[getKey()];
ready
extraction callback (static)
let callback = (obj) => obj.a;
let key_ = ["a", "b", "c", "d"][Math.floor(Math.random() * 3)]; // unused, to balance tests
let value = callback(object);
ready
extraction callback (dynamic, but fixed)
let key = "a";
let callback = (obj) => obj[key];
let key_ = ["a", "b", "c", "d"][Math.floor(Math.random() * 3)]; // unused, to balance tests
let value = callback(object);
ready
extraction callback (dynamic, changing)
let callback = (obj) => obj[["a", "b", "c", "d"][Math.floor(Math.random() * 3)]];
let value = callback(object);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.