Skip to content

Instantly share code, notes, and snippets.

@jasnell
Created April 25, 2021 22:25
Show Gist options
  • Save jasnell/0e2d60a5562dc7da9bd6c970ce07e106 to your computer and use it in GitHub Desktop.
Save jasnell/0e2d60a5562dc7da9bd6c970ce07e106 to your computer and use it in GitHub Desktop.
faster-path-set.js
Histogram {
min: 200,
max: 842751,
mean: 326.24267848,
exceeds: 0,
stddev: 512.5949230856029,
percentiles: SafeMap(30) [Map] {
0 => 200,
50 => 301,
75 => 401,
87.5 => 401,
93.75 => 401,
96.875 => 401,
98.4375 => 401,
99.21875 => 500,
99.609375 => 501,
99.8046875 => 601,
99.90234375 => 1400,
99.951171875 => 8600,
99.9755859375 => 11200,
99.98779296875 => 15896,
99.993896484375 => 22896,
99.9969482421875 => 40480,
99.99847412109375 => 73664,
99.99923706054688 => 81152,
99.99961853027344 => 84352,
99.99980926513672 => 89984,
99.99990463256836 => 98176,
99.99995231628418 => 106048,
99.99997615814209 => 116160,
99.99998807907104 => 127680,
99.99999403953552 => 140160,
99.99999701976776 => 157056,
99.99999850988388 => 201472,
99.99999925494194 => 366848,
99.99999962747097 => 842240,
100 => 842240
}
}
Histogram {
min: 100,
max: 778751,
mean: 289.044195505,
exceeds: 0,
stddev: 437.8095539818566,
percentiles: SafeMap(30) [Map] {
0 => 100,
50 => 300,
75 => 301,
87.5 => 401,
93.75 => 401,
96.875 => 401,
98.4375 => 401,
99.21875 => 401,
99.609375 => 401,
99.8046875 => 501,
99.90234375 => 1201,
99.951171875 => 1600,
99.9755859375 => 10896,
99.98779296875 => 11600,
99.993896484375 => 15896,
99.9969482421875 => 26000,
99.99847412109375 => 72640,
99.99923706054688 => 75072,
99.99961853027344 => 83456,
99.99980926513672 => 86848,
99.99990463256836 => 93248,
99.99995231628418 => 98752,
99.99997615814209 => 108352,
99.99998807907104 => 116800,
99.99999403953552 => 125376,
99.99999701976776 => 143488,
99.99999850988388 => 439808,
99.99999925494194 => 493568,
99.99999962747097 => 778240,
100 => 778240
}
}
'use strict'
function set (obj, path, value) {
if (obj == null)
throw new TypeError('obj is null or undefined');
const len = path.length
let i = 0, l = 0, prop = '';
while (i < len) {
if (path[i] === '.') {
prop = path.slice(l, i);
switch (prop.length) {
case 9:
if (prop === '__proto__' || prop === 'prototype')
return;
break;
case 11:
if (prop === 'constructor')
return;
break;
}
switch (typeof obj[prop]) {
case 'object':
obj = obj[prop];
break;
case 'undefined':
obj = obj[prop] = {};
break;
default:
return;
}
l = ++i;
continue;
}
i++
}
obj[path.slice(l, i)] = value
}
module.exports = set
/* eslint-disable no-console */
const {
createHistogram,
performance: {
timerify,
},
} = require('perf_hooks')
if (process.argv.includes('fast-path-set')) {
const h = createHistogram();
const set = timerify(require('./index'), { histogram : h } );
for (let i = 0; i < 100**4; i++) {
const obj = {}
set(obj, 'a.b.c.d', 1)
set(obj, 'd.c', 2)
}
console.log(h)
}
if (process.argv.includes('faster-path-set')) {
const h = createHistogram();
const set = timerify(require('./index2'), { histogram : h } );
for (let i = 0; i < 100**4; i++) {
const obj = {}
set(obj, 'a.b.c.d', 1)
set(obj, 'd.c', 2)
}
console.log(h)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment