Skip to content

Instantly share code, notes, and snippets.

@dariocravero
Created March 15, 2018 09:45
Show Gist options
  • Save dariocravero/1d8ee40965fdc19d711f01ada09b926a to your computer and use it in GitHub Desktop.
Save dariocravero/1d8ee40965fdc19d711f01ada09b926a to your computer and use it in GitHub Desktop.
access performance test
function a({ en, es, pl, fr, ch, ja }) {
return en
? 'hi'
: es
? 'hola'
: pl ? 'czezch' : fr ? 'bon jour' : ch ? 'ni hao' : ja ? 'wtf' : '';
}
const l = {
en: 'hi',
es: 'hola',
pl: 'czezch',
fr: 'bon jour',
ch: 'ni hao',
ja: 'wtf',
};
function b({ ll }) {
return l[ll];
}
function c(o) {
return l[o.ll];
}
const ls = new Map([
['en', 'hi'],
['es', 'hola'],
['pl', 'czezch'],
['fr', 'bon jour'],
['ch', 'ni hao'],
['ja', 'wtf'],
]);
function d({ ll }) {
return ls.get(ll);
}
const ll = { es: true, ll: 'es' };
console.time('a');
for (let i = 0; i < 1000; i++) {
a(ll);
}
console.timeEnd('a');
console.time('b');
for (let i = 0; i < 1000; i++) {
b(ll);
}
console.timeEnd('b');
console.time('d');
for (let i = 0; i < 1000; i++) {
d(ll);
}
console.timeEnd('d');
console.time('c');
for (let i = 0; i < 1000; i++) {
c(ll);
}
console.timeEnd('c');

This is a simple access performance test to determine which is the best way to access a locale value off an object.

To run it yourself, do node access-test.js or paste that into your browser's terminal:

a: 0.198ms
b: 0.080ms
d: 0.127ms
c: 0.098ms

b, simple objects holding keys seems to be the most performant approach overall

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment