简体   繁体   English

从对象数组构建对象的最高效方式

[英]Most performant way to build object from array of objects

Currently, I'm building an object from an array of objects with the following: 当前,我正在使用以下对象从对象数组构建对象:

var popOverOptions = {
  defaultOption: true
}

if (a) {
  options.push({a: "b"});
}

if (c) {
  options.push({c: "d"});
}

// Loop through array of objects
for (var i = 0; i < options.length; i++) {
   // Add objects in array to popoverOptions object
   for (key in options[i]) {
     popoverOptions[key] = options[i][key]
   }
}

I'm thinking this could be optimized and I'm curious if there is a better way to write this, possibly using .reduce(), .forEach() or some other method. 我在想可以对此进行优化,并且我很好奇是否有更好的方法来编写它,可能使用.reduce()、. forEach()或其他方法。

In ECMAScript 6, you can use 在ECMAScript 6中,您可以使用

Object.assign(popOverOptions, ...options);

You can optimise the loop itself like this: 您可以像这样优化循环本身:

for (var i=0, n=options.length; i<n; ++i) {

This reduces the number of times you need to access options.length which is slower than reading it from a local. 这减少了您访问options.length的次数,这比从本地读取它要慢。

A smaller optimisation: 较小的优化:

for (var i=options.length-1; i--; /* empty */) {

Source: http://www.phpied.com/extreme-javascript-optimization/ 来源: http : //www.phpied.com/extreme-javascript-optimization/

You can cache length for a slight speed improvement. 您可以缓存length以略微提高速度。

If the keys in your array are identical for each element, you can get twice the speed (in Chrome) by caching Object.keys(options[0]) , and iterating through that. 如果数组中的每个元素的键都相同,则通过缓存Object.keys(options[0])并对其进行迭代,可以获得两倍的速度(在Chrome中)。

Even if the keys aren't the same, you can still get approx. 即使密钥是不一样的,你仍然可以得到约。 25% increase in speed by iterating through Object.keys(options[i]) . 通过遍历Object.keys(options[i])可使速度提高25%。

 var options= []; for(var i = 0 ; i <= 1000 ; i++) { //create array of objects with random numbers options[i]= {}; for(var j = 0 ; j <= 5000 ; j++) { options[i][j]= Math.random(); } } var popOverOptions = {}; function keyin() { var timer= new Date(); for(var i = 0, leni = options.length ; i < leni; i++) { for(var key in options[i]) { popOverOptions[key] = options[i][key]; } } alert((new Date()-timer)/1000+' seconds'); } //keyin function objkeys(same) { var timer= new Date(), keys= Object.keys(options[0]); for(var i = 0, leni = options.length ; i < leni; i++) { if(!same) keys= Object.keys(options[i]); for(var key = 0, lenk = keys.length ; key < lenk ; key++) { popOverOptions[keys[key]] = options[i][keys[key]]; } } alert((new Date()-timer)/1000+' seconds'); } //objkeys 
 <button onclick="keyin()">key in options</button><br> <button onclick="objkeys(true)">Object.keys, same keys per element</button><br> <button onclick="objkeys(false)">Object.keys, different keys per element</button> 

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

相关问题 Javascript:对象数组到包含值数组的对象-最有效的方式? - Javascript: Array of objects to object containing arrays of values - most performant way? 爬取JSON对象数组的最有效方式 - Most performant way to crawl json object array 用唯一键将对象数组转换为对象的最有效方法是什么 - What is the most performant way to convert an Array of Object to an Object with unique keys 将一个数组中的值分配给嵌套数组的最有效方法 - Most performant way to assign values from one array into nested array 深度复制对象的最有效方式javascript - most performant way to deep copy objects javascript 高效的方法来过滤对象数组 - Performant way to filtering an array of objects 最有效的方式是通过另一个深度嵌套的对象数组对一个深度嵌套的对象数组进行排序 - Most performant way to sort a deeply nested array of objects by another deeply nested array of objects 在对象数组中更新 object 属性的最有效方法 - Most efficient way to update an object property within an array of objects 将对象数组转换为对象的最有效方法是什么 - What is the most efficient way to convert an array of objects to an object 将两个对象数组与 v8 和过滤器/查找进行比较的高性能方法 - Performant way to compare two array of objects with v8 & filter/find
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM