Skip to content

Commit 2c09633

Browse files
committed
os: pre-allocate the result array in cpus
1 parent cc7018e commit 2c09633

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

lib/os.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'use strict';
2323

2424
const {
25+
Array,
2526
ArrayPrototypePush,
2627
Float64Array,
2728
NumberParseInt,
@@ -140,10 +141,12 @@ function loadavg() {
140141
function cpus() {
141142
// [] is a bugfix for a regression introduced in 51cea61
142143
const data = getCPUs() || [];
143-
const result = [];
144+
const result = new Array(data.length / 7);
144145
let i = 0;
146+
let resultIndex = 0;
147+
145148
while (i < data.length) {
146-
ArrayPrototypePush(result, {
149+
result[resultIndex++] = {
147150
model: data[i++],
148151
speed: data[i++],
149152
times: {
@@ -153,7 +156,7 @@ function cpus() {
153156
idle: data[i++],
154157
irq: data[i++],
155158
},
156-
});
159+
};
157160
}
158161
return result;
159162
}

0 commit comments

Comments
 (0)