javascript - How are object properties implemented in Node.js -
i had array large amount of values iterating on find specific value this:
function havevalue(value) { for(var index in arr) { var prop = arr[index]; if(prop === value) { return true; } } return false; }
but occurred me possibly perform lookup more using object:
function havevalue(value) { return typeof obj[value] !== 'undefined'; }
however, have not seen of improvement in performance after doing this. i'm wondering, how object properties stored in javascript/node.js? iterated on in order find, same array? thought might implemented using hash table or something.
you've asked iliterating through array , if using object give better performance, using for in
loop array, , perfromance might better increased using for
loop or es6 includes method.
if arr
array , want check if contains value, use es6 includes()
method (docs here)
function havevalue(value) { return (arr.includes(value)); }
n.b. you'll need check if target browsers support , shim/pollyfill if not.
if dont want use es6 can loop for
loop
function havevalue(value) { for(var = 0; < arr.length; i++) { if(arr[i] === value) { return true; } } return false; }
in example using for in
loop objects not arrays. for
loop should give better perf when used on arrays
while size of object might effect perf when using string find value in object (like hashmap), effect might insignificant js engines internally optimised.
check jspef, selecting first of last property of object doesnt effect perfromance. (obviously effect may scale)
Comments
Post a Comment