Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: use Object.create(null) for dictionary object #3791

Closed
wants to merge 10 commits into from
4 changes: 2 additions & 2 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ function stylizeNoColor(str, styleType) {


function arrayToHash(array) {
var hash = {};
var hash = Object.create(null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not a Set instead?


array.forEach(function(val, idx) {
array.forEach(function(val) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

function arrayToHash(array) {
  return array.reduce((acc, val) => {
    acc[val] = true;
    return acc;
  }, Object.create(null));
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

hash[val] = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd check if val was __proto__ here and discard it if it was, I think. cc @bnoordhuis

});

Expand Down