Giter VIP home page Giter VIP logo

Comments (4)

markerikson avatar markerikson commented on June 2, 2024

Out of curiosity, what's the use case for Object.create(null) here? What are you doing with it in code?

from redux.

zhe-he avatar zhe-he commented on June 2, 2024

I use it just to create an empty object for storing data, and later I will add keys and values to this empty object. In some cases, it just makes it convenient for me to have fewer declarations, for example, declaring AnyObject explicitly.

const data = Object.create(null);
data.a = 1;

const data: AnyObject = {};
data.a = 1;

from redux.

markerikson avatar markerikson commented on June 2, 2024

@zhe-he : I'm okay with merging the fix, but tbh I'm not sure I see an actual reason to use Object.create(null) here :) the AnyObject example is actually shorter, and also more clear about what's going on.

from redux.

zhe-he avatar zhe-he commented on June 2, 2024

Well..., I'm not trying to convince you which method is better, I think any method is fine, it's just based on my personal habits.
I don't like explicit declarations, so I used the any returned by Object.create. At the same time, I personally think that the Object.create(null) loop will be a bit faster, so I used this, below is the time spent on testing.

image
// 2 lines
import { AnyObject } from "@/@types";
const data: AnyObject = {};

// 1 line
const data = Object.create(null);


// 2 lines or 9 lines
export type M = {
    name: string;
    value: string;
    child?: M[];
}
export type O = {
    [props: string]: M
}
// import { O } from "@/@types";
const map: O = {}

// 1 line
const map = Object.create(null);

from redux.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.