Created
March 6, 2019 15:35
-
-
Save pasaran/a669a5dda5315c9d4c1a982f9e3886ea to your computer and use it in GitHub Desktop.
Сравнение immutability-helper и jsetter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
Author
pasaran
commented
Mar 6, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment