memory-stats.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * @author mrdoob / http://mrdoob.com/
  3. * @author jetienne / http://jetienne.com/
  4. * @author paulirish / http://paulirish.com/
  5. */
  6. var MemoryStats = function (){
  7. var msMin = 100;
  8. var msMax = 0;
  9. var container = document.createElement( 'div' );
  10. container.id = 'stats';
  11. container.style.cssText = 'width:80px;opacity:0.9;cursor:pointer';
  12. var msDiv = document.createElement( 'div' );
  13. msDiv.id = 'ms';
  14. msDiv.style.cssText = 'padding:0 0 3px 3px;text-align:left;background-color:#020;';
  15. container.appendChild( msDiv );
  16. var msText = document.createElement( 'div' );
  17. msText.id = 'msText';
  18. msText.style.cssText = 'color:#0f0;font-family:Helvetica,Arial,sans-serif;font-size:9px;font-weight:bold;line-height:15px';
  19. msText.innerHTML= 'Memory';
  20. msDiv.appendChild( msText );
  21. var msGraph = document.createElement( 'div' );
  22. msGraph.id = 'msGraph';
  23. msGraph.style.cssText = 'position:relative;width:74px;height:30px;background-color:#0f0';
  24. msDiv.appendChild( msGraph );
  25. while ( msGraph.children.length < 74 ) {
  26. var bar = document.createElement( 'span' );
  27. bar.style.cssText = 'width:1px;height:30px;float:left;background-color:#131';
  28. msGraph.appendChild( bar );
  29. }
  30. var updateGraph = function ( dom, height, color ) {
  31. var child = dom.appendChild( dom.firstChild );
  32. child.style.height = height + 'px';
  33. if( color ) child.style.backgroundColor = color;
  34. }
  35. var perf = window.performance || {};
  36. // polyfill usedJSHeapSize
  37. if (!perf.memory){
  38. perf.memory = { usedJSHeapSize : 0 };
  39. }
  40. // support of the API?
  41. if( perf.memory.totalJSHeapSize === 0 ){
  42. console.warn('totalJSHeapSize === 0... performance.memory is only available in Chrome .')
  43. }
  44. // TODO, add a sanity check to see if values are bucketed.
  45. // If so, remind user to adopt the --enable-precise-memory-info flag.
  46. // open -a "/Applications/Google Chrome.app" --args --enable-precise-memory-info
  47. var lastTime = Date.now();
  48. var lastUsedHeap= perf.memory.usedJSHeapSize;
  49. return {
  50. domElement: container,
  51. update: function () {
  52. // refresh only 30time per second
  53. if( Date.now() - lastTime < 1000/30 ) return;
  54. lastTime = Date.now()
  55. var delta = perf.memory.usedJSHeapSize - lastUsedHeap;
  56. lastUsedHeap = perf.memory.usedJSHeapSize;
  57. var color = delta < 0 ? '#830' : '#131';
  58. var ms = perf.memory.usedJSHeapSize;
  59. msMin = Math.min( msMin, ms );
  60. msMax = Math.max( msMax, ms );
  61. msText.textContent = "Mem: " + bytesToSize(ms, 2);
  62. var normValue = ms / (30*1024*1024);
  63. var height = Math.min( 30, 30 - normValue * 30 );
  64. updateGraph( msGraph, height, color);
  65. function bytesToSize( bytes, nFractDigit ){
  66. var sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
  67. if (bytes == 0) return 'n/a';
  68. nFractDigit = nFractDigit !== undefined ? nFractDigit : 0;
  69. var precision = Math.pow(10, nFractDigit);
  70. var i = Math.floor(Math.log(bytes) / Math.log(1024));
  71. return Math.round(bytes*precision / Math.pow(1024, i))/precision + ' ' + sizes[i];
  72. };
  73. }
  74. }
  75. };