Skip to content

Instantly share code, notes, and snippets.

@koh110
Created Oct 26, 2020
Embed
What would you like to do?

配列のメモリ使用量確認

$ 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