如何优化两个对象数组的合并?

时间:2022-04-28 03:55:19

I would like to optimize my code for CPU and memory consumption. In my function I need to merge two arrays of object into one array. Like UNION, all the ids of the objects in the array have to be unique. I do not want to use third party libraries like Underscore.

我想优化我的CPU和内存消耗代码。在我的函数中,我需要将两个对象数组合并到一个数组中。与UNION一样,数组中对象的所有id都必须是唯一的。我不想使用像Underscore这样的第三方库。

This is my function:

这是我的功能:

var presentation_slides = [
  {
    "id": "2",
    "type": "results"
  },
  {
    "id": "1",
    "type": "slide"
  },
  {
    "id": "4",
    "type": "questions"
  }]

for(var i = 0; i < new_length; i++) {
    var my_slide = presentation_slides.filter(function (obj) { return obj.id == i })[0]
    if(!my_slide) {
        presentation_slides.push({"id": i, "type": "slide"});
    }
}

OUTPUT:

OUTPUT:

var presentation_slides = [
  {
    "id": "2",
    "type": "results"
  },
  {
    "id": "1",
    "type": "slide"
  },
  {
    "id": "4",
    "type": "questions"
  },
  {
    "id": 0,
    "type": "slide"
  },
  {
    "id": 3,
    "type": "slide"
  }]

Thank you!

谢谢!

EDIT:

编辑:

Some tests for comparison: http://jsperf.com/how-can-be-optimized-merging-of-two-arrays-of-objects

一些比较测试:http://jsperf.com/how-can-be-optimized-merging-of-two-arrays-of-objects

3 个解决方案

#1


1  

you can use some instead of filter, i have added a code snipet to your jsperf, which is faster...

你可以使用一些而不是过滤器,我已经为你的jsperf添加了一个代码snipet,这更快......

check this: .....jsperf.....

检查一下:..... jsperf .....

for (var i = 0; i < new_length; i++) {

    if (!presentation_slides.some(function(obj) { return obj.id == i}))
        presentation_slides.push({ "id": i, "type": "slide" });
}

#2


0  

If you just want to merge two arrays, then you can use Array.concat():

如果您只想合并两个数组,则可以使用Array.concat():

var array1 = [
    { id: 1, type: "orange" },
    {id: 3, type: "apple" },
    { id: 4, type: "banana" } ],
    array2 = [
        { id: 8, type: "strawberry" },
        { id: 9, type: "raspberry" }
    ];

var array3 = array1.concat(array2);

array3 will contain all the elements from array1 and array2.

array3将包含array1和array2中的所有元素。

This will not check to see if the IDs are unique though.

这不会检查ID是否是唯一的。

#3


0  

also, reverse loops are a little faster:

另外,反向循环更快一点:

var i = new_length;
while (i--) {
    if (!presentation_slides.some(function(obj) { return obj.id == i })) {
        presentation_slides.push({ "id": i, "type": "slide" });
    }
}

check this ...jspref...

检查一下... jspref ...

#1


1  

you can use some instead of filter, i have added a code snipet to your jsperf, which is faster...

你可以使用一些而不是过滤器,我已经为你的jsperf添加了一个代码snipet,这更快......

check this: .....jsperf.....

检查一下:..... jsperf .....

for (var i = 0; i < new_length; i++) {

    if (!presentation_slides.some(function(obj) { return obj.id == i}))
        presentation_slides.push({ "id": i, "type": "slide" });
}

#2


0  

If you just want to merge two arrays, then you can use Array.concat():

如果您只想合并两个数组,则可以使用Array.concat():

var array1 = [
    { id: 1, type: "orange" },
    {id: 3, type: "apple" },
    { id: 4, type: "banana" } ],
    array2 = [
        { id: 8, type: "strawberry" },
        { id: 9, type: "raspberry" }
    ];

var array3 = array1.concat(array2);

array3 will contain all the elements from array1 and array2.

array3将包含array1和array2中的所有元素。

This will not check to see if the IDs are unique though.

这不会检查ID是否是唯一的。

#3


0  

also, reverse loops are a little faster:

另外,反向循环更快一点:

var i = new_length;
while (i--) {
    if (!presentation_slides.some(function(obj) { return obj.id == i })) {
        presentation_slides.push({ "id": i, "type": "slide" });
    }
}

check this ...jspref...

检查一下... jspref ...