UNPKG

vapr-compress

Version:
33 lines (29 loc) 998 B
'use strict'; const nodemark = require('nodemark'); const benchmark = (n, f) => console.log(`${n} x ${nodemark(f, typeof setup === 'function' ? setup : undefined).toString('nanoseconds')}`); const array = ['gzip', 'deflate', 'identity']; const set = new Set(['gzip', 'deflate', 'identity']); const map = new Map([['gzip'], ['deflate'], ['identity']]); const obj = { gzip: true, deflate: true, identity: true }; const nullobj = Object.assign(Object.create(null), { gzip: true, deflate: true, identity: true }); let result, test; const setup = () => { const str = Math.random() < 0.9 ? 'br' : 'gzip'; test = Buffer.from(str).toString(); }; benchmark('array', () => { result = array.includes(test); }); benchmark('set', () => { result = set.has(test); }); benchmark('map', () => { result = map.has(test); }); benchmark('obj', () => { result = obj.hasOwnProperty(test); }); benchmark('objnull', () => { result = obj[test] ? true : false; }); console.log(String(result).replace(/[^~]/g, ''));