In javascript to remove 1 element from beginning of an array shift and slice functions performance vary with size. Shift time grows more than that of slice. After 250K change is significant. Showing that shift should be preferred over slice.
Nodejs Code:
Nodejs Code:
var r; var arr = []; var n, s = 1; var t, t1, d, d1; var max_size = 999999999999999; console.log('size, shift, slice'); while (s < max_size) { n = s; while (n) { arr.push(n--); } t = process.hrtime(); arr.shift(); r = arr; d = process.hrtime(t); t1 = process.hrtime(); r = arr.slice(1); d1 = process.hrtime(t1); console.log([s, d[0] * 1e9 + d[1], d1[0] * 1e9 + d1[1]].join(',')); s++; }Results (in nanoseconds):
size shift Slice(1) 1 7138 2216 2 9384 2198 4 2675 1449 8 2253 1313 16 1958 1370 32 2285 1261 64 1999 1211 128 1996 1400 256 2244 1692 512 2541 2165 1024 2635 3240 2048 2904 5031 4096 3039 26260 8192 3263 53801 16384 5012 134611 32768 4576 275972 65536 148325 421068 131072 271896 598540 262144 528817 1213684 524288 1095032 2219610 1048576 2880932 5821282 2097152 7697251 24937937 4194304 14904224 42081247 8388608 29666453 87721218
No comments:
Post a Comment