Index array or simple filter

Benchmark created on


Setup

const elems = [
    {"uid": "01", "type": "b", "val": 10},
    {"uid": "02", "type": "b", "val": 10},
    {"uid": "03", "type": "b", "val": 8},
    {"uid": "04", "type": "b", "val": 7},
    {"uid": "05", "type": "a", "val": 3},
    {"uid": "06", "type": "b", "val": 4},
    {"uid": "07", "type": "a", "val": 4},
    {"uid": "08", "type": "b", "val": 4},
    {"uid": "09", "type": "a", "val": 3},
    {"uid": "10", "type": "a", "val": 7},
    {"uid": "11", "type": "b", "val": 1},
    {"uid": "12", "type": "b", "val": 5},
    {"uid": "13", "type": "a", "val": 6},
    {"uid": "14", "type": "b", "val": 4},
    {"uid": "15", "type": "b", "val": 6},
    {"uid": "16", "type": "b", "val": 2},
    {"uid": "17", "type": "a", "val": 2},
    {"uid": "18", "type": "a", "val": 1},
    {"uid": "19", "type": "a", "val": 5},
    {"uid": "20", "type": "b", "val": 10}
];

const index = {'a': [], 'b': []}
elems.forEach(function(el, idx) {
	index[el.type].push(idx)
})

Test runner

Ready to run.

Testing in
TestOps/sec
naive
const allAs = elems.filter(e => e.type === 'a');
//const els  = true;
ready
indexed

const allAs = index['a'];
const allAlElems = allAs.map(idx => elems[idx])

//const els = false
ready

Revisions

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