Uh oh!
There was an error while loading. Please reload this page.
util: use Object.create(null) for dictionary object - #3791
Conversation
Fixes#3788 `arrayToHash()` needs to use `Object.create(null)` for its dictionary object.
evanlucas
commented
Nov 12, 2015
Could use a test |
Fishrock123
commented
Nov 12, 2015
Note: |
Fishrock123
commented
Nov 12, 2015
See also: #728 |
Fishrock123
commented
Nov 12, 2015
Probably better would be to special-case |
There was a problem hiding this comment.
You'd check if val was __proto__ here and discard it if it was, I think. cc @bnoordhuis
add if val is `__proto__`, use `Object.create(null);`
There was a problem hiding this comment.
Why creating a new hash in each iteration?
Fishrock123
commented
Nov 12, 2015
@JungMinu please run |
pmq20
commented
Nov 12, 2015
Also rebasing multiple commits into one in a single MR is more preferable. |
if array contains __proto__, use Object.create(null)
YurySolovyov
commented
Nov 12, 2015
also make sure you follow coding style (spaces, braces, etc.) |
Beautify codes
There was a problem hiding this comment.
@YuriSolovyov Hmm, It was there before I made a PR,
I speculate that it could be used
There was a problem hiding this comment.
it is not used anywhere in the scope of the function, IMO it is safe to drop it
trevnorris
commented
Nov 12, 2015
@Fishrock123 @JungMinu Sorry for the confusion here. Let's simply set |
util: use Object.create(null) for dictionary object
Fishrock123
commented
Nov 12, 2015
Fair enough. My bad. |
jasnell
commented
Nov 12, 2015
LGTM |
trevnorris
commented
Nov 12, 2015
if CI is happy, LGTM |
cjihrig
commented
Nov 12, 2015
LGTM. Good to know that we may be able to use this in other place in the code too. |
There was a problem hiding this comment.
Would we want to continue to use arrow functions for inline anons?
array.forEach(val=>hash[val]=true);and/or convert this into a single reduce expression?
functionarrayToHash(array){returnarray.reduce((acc,val)=>{acc[val]=true;returnacc;},Object.create(null));}There was a problem hiding this comment.
Using reduce() is a bit misleading since nothing is actually being reduced, and I don't think turning it into a single expression is helpful in this case.
As for using arrow functions, personally only think they must be used to prevent var self = this; from happening, but also wouldn't be opposed if it were used. IOW, if it does change then cool, but it's not a blocker.
There was a problem hiding this comment.
Ok cool. Yeah I guess I agree, the naming of Array.prototype.reduce is definitely a bit awkward here. Maybe the general arrow function stance is also worth a larger style/convention conversation elsewhere? Thanks for the input!
There was a problem hiding this comment.
Ironically @JungMinu has already started some of this :) #3622
Hopefully we'll be able to finish conversion of other applicable cases, but it's not high priority. Don't think the CTC would feel the need to make a stance "official" but if the rest of the code is following some convention then we can point to that as an example going forward.
Fishrock123
commented
Nov 12, 2015
CI: https://ci.nodejs.org/job/node-test-pull-request/710/ (Continuous Integration test run) |
There was a problem hiding this comment.
This adds a redundant semicolon.
jasnell
commented
Nov 13, 2015
CI is red but failures look unrelated. |
jasnell
commented
Nov 13, 2015
@JungMinu .. can I ask you to please squash the commits? |
thefourtheye
commented
Nov 14, 2015
I just used this script to compare the performance. 'use strict';consttimes=10e6;console.time('Object.create');for(vari=0;i<times;i+=1){letobj=Object.create(null);}console.timeEnd('Object.create');console.time('Object Literal');for(vari=0;i<times;i+=1){letobj={};}console.timeEnd('Object Literal');And it looks like |
JungMinu
commented
Nov 15, 2015
@jasnell Sure, I will squash the commits soon :) |
jasnell
commented
Nov 15, 2015
Thank you!
|
trevnorris
commented
Nov 15, 2015
@thefourtheye If you look at the IR generated by the optimizing compiler you'll find much of the second call has been optimized out completely. Also, if you look at actual execution time you'll see that the savings is in the low double digit nanoseconds. The call is sufficiently fast for how it's used here. |
thefourtheye
commented
Nov 15, 2015
@trevnorris Yup, the difference is not pronounced much. Probably, I'll have a shot at #2350 again. |
JungMinu
commented
Nov 15, 2015
@jasnell@trevnorris Thanks a lot 😄 |
JungMinu
commented
Nov 15, 2015
@jasnell@trevnorris Sorry, because of the conflicts in my branch, I submitted a new PR, #3831 |
WebReflection
commented
Mar 15, 2016
has anyone tested |
Fixes#3788
arrayToHash()needs to useObject.create(null)for its dictionary object.