Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to keep track of how many arrays are created in Javascript?

I want to be able to keep track of how many arrays a certain program uses, so that I can figure out if unnecessary memory has been allocated.The problem I am running into is Javascript's construct that array literal [] does not trigger Array#constructor. Is there another way for me to keep track of array usage? ES5 and/or ES6 and beyond.

var arrayConstructor = Array // store reference
window.arrCount = 0
window.Array = function() {
  arrCount++;
  return new Array();
}

var arr1 = new Array
window.arrCount // 1

var arr2 = []
window.arrCount // 1 : NOT WORKING!
like image 491
user2167582 Avatar asked Nov 02 '16 04:11

user2167582


1 Answers

Nearly every web browser has a javascript profiler. The purpose of a profiler is to keep track of memory usage, frame rendering speeds, CPU usage, etc.

  • Open the Chrome Developer Tools (F12)
  • Click the "Timeline" tab
  • Make sure "Memory" is checked in the bar named "Capture"
  • Press the record button in the top left
  • Refresh the page
  • Stop recording once the page has loaded
  • Look at the "JS Heap" graph for a breakdown on where your memory went

For example, Chrome's memory profiler looks like this when recording the loading of the Stack Overflow's Top Questions page.

enter image description here

like image 51
Soviut Avatar answered Oct 06 '22 10:10

Soviut