I have an array declared as
var arr = new Array();
Then i have an array of objects which are returned by Server. And each object in this array has three fields(always). I have to loop through this and add to the arr array conditionally.
Since this arr is not pre-allocated, it hits performance for large number in the main array.
Is there any way i can pre-allocate the arr array after i get the main response array so that i can avoid this performance issue?
Also how do i get the size of the object?
Thanks.
I have an array declared as
var arr = new Array();
Then i have an array of objects which are returned by Server. And each object in this array has three fields(always). I have to loop through this and add to the arr array conditionally.
Since this arr is not pre-allocated, it hits performance for large number in the main array.
Is there any way i can pre-allocate the arr array after i get the main response array so that i can avoid this performance issue?
Also how do i get the size of the object?
Thanks.
Share Improve this question edited May 20, 2015 at 14:24 max 102k15 gold badges113 silver badges176 bronze badges asked May 20, 2015 at 14:23 aneeshereaneeshere 5496 silver badges18 bronze badges 2-
1
don't worry about it, seriously, this isn't C, the whole point of higher level languages is not having to worry about this stuff. For your looping problem, just call
Array#filter
on your returned array. – Willem D'Haeseleer Commented May 20, 2015 at 14:25 - 1 @TasosK. gamealchemist.wordpress./2013/05/01/… – aneeshere Commented May 20, 2015 at 14:52
3 Answers
Reset to default 12Suppose you have 10 objects, and you are going to pass three values from each object to an array. You can initialize your array with length 30 (10*3) by passing the integer 30 to the Array constructor as such:
var numObjects = 10;
var myArray = new Array(3*numObjects);
Please refer to my jsperf benchmark for a proof of the performance gained. In short summary, pre-sizing your array is ~25% faster in Firefox 38, ~81% faster in Chrome 42, and ~16% faster in Internet Explorer 11. Numbers will vary by the individual's experience who runs these benchmarks, but the trend will remain consistent. The optimal performance will result from pre-sizing your arrays.
http://jsperf./array-growth-dynamic-vs-preset
A more thorough discussion of this topic has occured here on SO at
How to initialize an array's length in javascript?
You can filter your array using filter
function like the below example
var result = [
{
age: 15
},
{
age: 21
},
{
age: 25
}
];
function isGreaterThan20(obj) {
return obj.age > 20;
}
var arr = result.filter(isGreaterThan20);
// arr bees [{ age: 21}, { age: 25}]
If you need to pre-allocate an array with defined size, use new Array(size)
Thank whatever deity you believe in (or not) that Javascript does not have any direct access to memory allocation. That would have been truly horrible considering the quality of much of the JS littering the interwebs.
Javascript will by itself allocate memory to arrays on creation and reclaim the memory when it is garbage collected. Pre-Filling an array will have no positive effect on memory usage or performance.
Edit: I was wrong. See @ThisClark's answer.
MDN has a pretty good article on how memory management and GC work in javascript.