简体   繁体   English

直接从 object 访问属性?

[英]Accessing properties from an object directly?

In Javascript, if we have a scenario in which there is a large object and we need to access its properties then which approach is better?在 Javascript 中,如果我们有一个场景,其中有一个很大的 object,我们需要访问它的属性,那么哪种方法更好?

Suppose we have this object:假设我们有这个 object:

let largeObj = {
  items: {
    item: [
      {
        id: "0001",
        type: "donut",
        name: "Cake",
        ppu: 0.55,
        batters: {
          batter: [
            {
              id: "1001",
              type: "Regular"
            },
            {
              id: "1002",
              type: "Chocolate"
            },
            {
              id: "1003",
              type: "Blueberry"
            },
            {
              id: "1004",
              type: "Devil's Food"
            }
          ]
        },
        topping: [
          {
            id: "5001",
            type: "None"
          },
          {
            id: "5002",
            type: "Glazed"
          },
          {
            id: "5005",
            type: "Sugar"
          },
          {
            id: "5007",
            type: "Powdered Sugar"
          },
          {
            id: "5006",
            type: "Chocolate with Sprinkles"
          },
          {
            id: "5003",
            type: "Chocolate"
          },
          {
            id: "5004",
            type: "Maple"
          }
        ]
      }
    ]
  }
}

and we want to create firstBatterAndTopping object which has first batter and first topping data.我们要创建firstBatterAndTopping object ,它具有第一击球手和第一击球数据。 Now, if we access properties of this object in below three ways, which approach is better in terms of performance.现在,如果我们通过以下三种方式访问此 object 的属性,就性能而言,哪种方法更好。 Is there any difference in these or these will perform the same.这些是否有任何区别,或者这些将执行相同的操作。 Or is there any other scenario like this in which this will impact performance?或者还有其他类似的情况会影响性能吗?

let firstBatterAndTopping = {
    batterId: largeObj.items.item[0].batters.batter[0].id,
    batterType: largeObj.items.item[0].batters.batter[0].type,
    toppingId: largeObj.items.item[0].topping[0].id,
    toppingType: largeObj.items.item[0].topping[0].type
}

or或者

let firstItem = largeObj.items.item[0];
let firstBatterAndTopping = {
    batterId: firstItem.batters.batter[0].id,
    batterType: firstItem.batters.batter[0].type,
    toppingId: firstItem.topping[0].id,
    toppingType: firstItem.topping[0].type
}

or或者

let firstItem = largeObj.items.item[0];
let firstBatter = firstItem.batters.batter[0];
let firstTopping = firstItem.topping[0]
let firstBatterAndTopping = {
    batterId: firstBatter.id,
    batterType: firstBatter.type,
    toppingId: firstTopping.id,
    toppingType: firstTopping.type
}

Try immer out.尝试沉浸 Is a javascript library very useful for managing complex objects with a lot of nests: Following a simple scenario: javascript 库对于管理具有大量嵌套的复杂对象非常有用:遵循一个简单的场景:

import produce from "immer"

const baseState = [
    {
        title: "Learn TypeScript",
        done: true
    },
    {
        title: "Try Immer",
        done: false
    }
]

const nextState = produce(baseState, draftState => {
    draftState.push({title: "Tweet about it"})
    draftState[1].done = true
})
// the new item is only added to the next state,
// base state is unmodified
expect(baseState.length).toBe(2)
expect(nextState.length).toBe(3)

// same for the changed 'done' prop
expect(baseState[1].done).toBe(false)
expect(nextState[1].done).toBe(true)

// unchanged data is structurally shared
expect(nextState[0]).toBe(baseState[0])
// ...but changed data isn't.
expect(nextState[1]).not.toBe(baseState[1])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM