te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Pre-allocate memory to array of objects - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pre-allocate memory to array of objects - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 12

Suppose 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.

发布评论

评论列表(0)

  1. 暂无评论