Tuesday, August 8, 2017

Javascript Memory Limit and Usage in Chrome



<script>
// Thank you stackoverflow user B. Gruenbaum
console.log('performance.memory.jsHeapSizeLimit='+performance.memory.jsHeapSizeLimit);
// will give you the JS heap size
console.log('performance.memory.usedJSHeapSize='+performance.memory.usedJSHeapSize);
// how much you're currently using
</script>

Slightly off topic from post title, the following may be an issue in the problem set being thought on in regards to memory limits: JSON object sizes.
In my scenario I had static reference data in CSV format. I converted to JSON files ( e.g. materials.json).  Here is standalone PHP script you can place in directory of JSON files and get file sizes and javascript memory info all together.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1"></meta>
<meta name="format-detection" ></meta>
<title>JS Memory Size</title>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<?php
foreach(glob('*.json') as $file) {
   print "<script type='text/javascript' src='".$file."' ></script>".PHP_EOL;
}
?>

<script>
$(document).ready(function(){
  console.log('performance.memory.jsHeapSizeLimit='+performance.memory.jsHeapSizeLimit);
  // will give you the JS heap size
  console.log('performance.memory.usedJSHeapSize='+performance.memory.usedJSHeapSize);
  // how much you're currently using
  $('#jsHeapSizeLimit').text(performance.memory.jsHeapSizeLimit);
  $('#usedJSHeapSize').text(performance.memory.usedJSHeapSize);
});
</script>
<style>
td {
align:left;
border:1px dotted #000000;
padding:5px;
}
</style>
</head>
<body>
<h1>JS Memory Size</h1>
<table><tr>
<th>Size</th><th>Info</th>
</tr>
 <td id='jsHeapSizeLimit'></td><td>Size Limit</td>
</tr><tr>
 <td id='usedJSHeapSize'></td><td>Currently Used</td>
</tr>
<?php
foreach(glob('*.json') as $file) {
   print "<tr><td>".filesize($file)."</td><td>".$file."</td></tr>".PHP_EOL;
}
?>
</table>
</body>
</html>
<?php
die();
?>