javascript(ES6):比“ for循环”更有效的方法吗?

时间:2018-10-16 23:27:41

标签: javascript reactjs for-loop react-native

这不是重复项。请在下面查看我的评论!

在ES6中,有人知道比 for循环更有效的解决方案吗?

我写了以下内容,它们缺乏性能。有什么改进想法吗?高度赞赏。

基本上,我有一个关于汽车的对象和一个关于用户偏好的数组。预期的行为是将所有相关的汽车名称放入一个数组。

用户可以提供任意数量的首选项。如果在首选项中提到了所有规格,则仅应推送汽车名称。因此,某些首选项将是“剩菜剩饭”。

因此,在以下示例中,本田出现了,但宝马没有出现,这是预期的(但行为很慢)。

// Car objects
const cars = [{
    name: "Honda",
    category: "eco",
    specs: {
      0: "green",
      1: "fast",
      2: "automatic"
    }
  },
  {
    name: "BMW",
    category: "sport",
    specs: {
      0: "blue",
      1: "fast",
      2: "automatic"
    }
  }
]

// User preferences
const preferences = ["green", "fast", "4x4", "automatic", "panorama"]

// function to get length/amount of car specifications
function objsize(Myobj) {
  var osize = 0,
    key;
  for (key in Myobj) {
    if (Myobj.hasOwnProperty(key)) osize++;
  }
  return Object(osize);
};


//function to check if ALL specifications are included in the user preferences
function checkSpecs(spec_item) {
  return preferences.includes(spec_item)
}

// main function
function filter_func() {

  //final results
  let matched_cars = []


  for (i = 0; i < objsize(cars); i++) {

    let specs_collector = []

    for (j = 0; j < objsize(cars[i].specs); j++) {
      specs_collector.push(cars[i].specs[j])
    }

    if (specs_collector.every(checkSpecs) === true) {
      matched_cars.push(cars[i].name)
      specs_collector = []
    }

  }
  console.log(matched_cars)
}

filter_func()

3 个答案:

答案 0 :(得分:9)

您无法避免查看每辆汽车,也无法避免查看汽车中的每个规格,因为您要测试每个规格。您可以每次使用Set来避免遍历首选项。

所以这可能更快,也可能不会更快,但是它更简单易懂,因为代码几乎读成英语:过滤器车,每个参数都在首选项中:

// Car objects
const cars = [{
    name: "Honda",
    category: "eco",
    specs: ["green", "fast","automatic"]
    },
  {
    name: "BMW",
    category: "sport",
    specs: ["blue", "fast","automatic"]
    }
]

const preferences = new Set(["green", "fast", "4x4", "automatic", "panorama"])

let filtered = cars.filter(car => car.specs.every(spec => preferences.has(spec)))
console.log(filtered)

答案 1 :(得分:2)

-编辑-

在OP中使用数据:

const array_intersect = (a, b) => a.filter( i => (b.indexOf(i) >= 0) )
const a_contains_b = (a, b) => array_intersect(a, b).length == b.length

var cars = [{
    name: "Honda",
    category: "eco",
    specs: ["green", "fast", "automatic"]
  },
  {
    name: "BMW",
    category: "sport",
    specs: ["blue", "fast", "automatic"]
  }
]

const preferences = ["green", "fast", "4x4", "automatic", "panorama"]

let filtered = cars.filter(car => a_contains_b(preferences, car.specs))
console.log(filtered);

答案 2 :(得分:2)

无法逃避至少一个循环。您始终必须遍历所有汽车,无论是使用for ...还是使用诸如array.filter()的其他构造。但是还有另一种提高性能的方法。您可以使用位掩码。这将需要更改汽车对象的数据结构,以便每辆汽车已经包含与其规格相对应的位掩码,并且当用户选择所需的规格时,同样应添加规格代码。 (但是,我怀疑这可能太麻烦了,收效甚微。)

// Let's pretend there are preset binary digits corresponding 
// to each one of the available preferences:
//
//     "blue" => 1
//     "green" => 2
//     "red" => 4
//     "fast" => 8
//     "slow" => 16
//     "automatic" => 32
//     "4x4"  => 64
//     "panorama" => 128
//
// You would encode this into the data before processing

var cars = [{
    name: "Honda",
    category: "eco",
    specs: ["green", "fast", "automatic"],
    bin_specs: 42 // 2 + 8 + 32
  },
  {
    name: "BMW",
    category: "sport",
    specs: ["blue", "fast", "automatic"],
    bin_specs: 41 // 1 + 8 + 32
  }
]

const preferences = ["green", "fast", "4x4", "automatic", "panorama"]
const bin_preferences = 234 // 2 + 8 + 64 + 32 + 128]

let filtered = cars.filter(car => (car.bin_specs & bin_preferences) === car.bin_specs)

console.log(filtered);