Skip to content

Instantly share code, notes, and snippets.

@pasaran
Created November 26, 2018 11:19
Show Gist options
  • Save pasaran/357f9066dbe01de51d282ba2c4c9b3f9 to your computer and use it in GitHub Desktop.
Save pasaran/357f9066dbe01de51d282ba2c4c9b3f9 to your computer and use it in GitHub Desktop.
const no = require( 'nommon' );
const update = require( 'immutability-helper' );
const data = {
foo: {
bar: {
quu: [ 1, 2, 3, 4, 5 ],
},
status: false,
},
};
const index = 2;
const N = 100000;
let r;
console.time( 'immutability-helper' );
for ( let i = 0; i < N; i++ ) {
r = update( data, {
foo: {
bar: {
quu: {
[ index ]: {
$set: 42,
},
},
},
status: {
$set: true,
},
},
} );
}
console.timeEnd( 'immutability-helper' );
// console.log( JSON.stringify( r, null, 4 ) );
const js1 = no.jsetter( '.foo.bar.quu[ index ]' );
const js2 = no.jsetter( '.foo.status' );
console.time( 'nommon1' );
for ( let i = 0; i < N; i++ ) {
r = js1( data, { index: index }, 42 );
r = js2( r, null, true );
}
console.timeEnd( 'nommon1' );
// console.log( JSON.stringify( r, null, 4 ) );
console.time( 'nommon2' );
for ( let i = 0; i < N; i++ ) {
r = jsetter( {
'.foo.bar.quu[ index ]': 42,
'.foo.status': true,
}, data, { index: index } );
}
console.timeEnd( 'nommon2' );
// console.log( JSON.stringify( r, null, 4 ) );
// В nommon нет сейчас такого хелпера, так что пока так.
//
function jsetter( setters, data, vars) {
let r = data;
for ( let key in setters ) {
r = no.jsetter( key )( r, vars, setters[ key ] );
}
return r;
}
@pasaran
Copy link
Author

pasaran commented Nov 26, 2018

$ node test.js
immutability-helper: 996.502ms
nommon1: 198.095ms
nommon2: 259.303ms

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