-
Notifications
You must be signed in to change notification settings - Fork 29.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lib: freelist: use .pop() for allocation
Array#pop() is known to be faster than Array#shift(). To be exact, it's O(1) vs. O(n). In this case there's no difference from which side of the "pool" array the object is retrieved, so .pop() should be preferred. PR-URL: #2174 Reviewed-By: mscdex - Brian White <[email protected]> Reviewed-By: jasnell - James M Snell <[email protected]> Reviewed-By: ofrobots - Ali Ijaz Sheikh <[email protected]>
- Loading branch information
Showing
3 changed files
with
41 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
var common = require('../common.js'); | ||
var FreeList = require('internal/freelist').FreeList; | ||
|
||
var bench = common.createBenchmark(main, { | ||
n: [100000] | ||
}); | ||
|
||
function main(conf) { | ||
var n = conf.n; | ||
var poolSize = 1000; | ||
var list = new FreeList('test', poolSize, Object); | ||
var i; | ||
var j; | ||
var used = []; | ||
|
||
// First, alloc `poolSize` items | ||
for (j = 0; j < poolSize; j++) { | ||
used.push(list.alloc()); | ||
} | ||
|
||
bench.start(); | ||
|
||
for (i = 0; i < n; i++){ | ||
// Return all the items to the pool | ||
for (j = 0; j < poolSize; j++) { | ||
list.free(used[j]); | ||
} | ||
|
||
// Re-alloc from pool | ||
for (j = 0; j < poolSize; j++) { | ||
list.alloc(); | ||
} | ||
} | ||
|
||
bench.end(n); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters