Skip to content

Instantly share code, notes, and snippets.

@pasaran
Created March 6, 2019 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pasaran/a669a5dda5315c9d4c1a982f9e3886ea to your computer and use it in GitHub Desktop.
Save pasaran/a669a5dda5315c9d4c1a982f9e3886ea to your computer and use it in GitHub Desktop.
Сравнение immutability-helper и jsetter
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 Mar 6, 2019

node test.js
immutability-helper: 982.106ms
nommon1: 181.661ms
nommon2: 262.281ms

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