Skip to content

Instantly share code, notes, and snippets.

@koh110
Created October 26, 2020 12:02
Show Gist options
  • Save koh110/4c77dbebccfc074ac7bb1764423d6803 to your computer and use it in GitHub Desktop.
Save koh110/4c77dbebccfc074ac7bb1764423d6803 to your computer and use it in GitHub Desktop.

配列のメモリ使用量確認

$ node memory_new_array.js 
[
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1,
  ... 9900 more items
]
Heap: 2840104 bytes

$ node memory_new_array2.js
[
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1, 1, 1, 1,
  ... 9900 more items
]
Heap: 2922088 bytes


const TotalLanes = 10000;
const createLaneMap = (initial) => {
return new Array(TotalLanes).fill(initial)
}
const arr = createLaneMap(1)
console.log(arr)
const heapUsed = process.memoryUsage().heapUsed
console.log('Heap:', heapUsed, 'bytes')
const TotalLanes = 10000;
const createLaneMap = (initial) => {
const arr = []
for (let i = 0; i < TotalLanes; i++) {
arr.push(initial)
}
return arr
}
const arr = createLaneMap(1)
console.log(arr)
const heapUsed = process.memoryUsage().heapUsed
console.log('Heap:', heapUsed, 'bytes')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment