Bug 728780 - Define String.prototype.startsWith and use it in about:memory. r=jschoenick.
authorNicholas Nethercote <nnethercote@mozilla.com>
Mon, 20 Feb 2012 14:42:22 -0800
changeset 87266 621528be5df8c9862d390918d3fb2b214c373d32
parent 87265 ec21e7e02464f60cabd31bdedb126fd9962dd4a1
child 87267 027f56e65a84cba315935f13c9a71edf6214edc0
push id22103
push userbmo@edmorley.co.uk
push dateTue, 21 Feb 2012 12:01:45 +0000
treeherdermozilla-central@4038ffaa5d82 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjschoenick
bugs728780
milestone13.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 728780 - Define String.prototype.startsWith and use it in about:memory. r=jschoenick.
toolkit/components/aboutmemory/content/aboutMemory.js
--- a/toolkit/components/aboutmemory/content/aboutMemory.js
+++ b/toolkit/components/aboutmemory/content/aboutMemory.js
@@ -58,16 +58,20 @@ const UNITS_PERCENTAGE       = Ci.nsIMem
 
 // Because about:memory and about:compartments are non-standard URLs,
 // location.search is undefined, so we have to use location.href here.
 const gVerbose = location.href === "about:memory?verbose" ||
                  location.href === "about:compartments?verbose";
 
 let gChildMemoryListener = undefined;
 
+// This is a useful function and an efficient way to implement it.
+String.prototype.startsWith =
+  function(s) { return this.lastIndexOf(s, 0) === 0; }
+
 //---------------------------------------------------------------------------
 
 // Forward slashes in URLs in paths are represented with backslashes to avoid
 // being mistaken for path separators.  Paths/names where this hasn't been
 // undone are prefixed with "unsafe"; the rest are prefixed with "safe".
 function flipBackslashes(aUnsafeStr)
 {
   return aUnsafeStr.replace(/\\/g, '/');
@@ -96,20 +100,20 @@ function addChildObserversAndUpdate(aUpd
   gChildMemoryListener = aUpdateFn;
   os.addObserver(gChildMemoryListener, "child-memory-reporter-update", false);
  
   gChildMemoryListener();
 }
 
 function onLoad()
 {
-  if (location.href.indexOf("about:memory") === 0) {
+  if (location.href.startsWith("about:memory")) {
     document.title = "about:memory";
     onLoadAboutMemory();
-  } else if (location.href.indexOf("about:compartment") === 0) {
+  } else if (location.href.startsWith("about:compartment")) {
     document.title = "about:compartments";
     onLoadAboutCompartments();
   } else {
     assert(false, "Unknown location");
   }
 }
 
 function onUnload()
@@ -447,32 +451,32 @@ Report.prototype = {
       this._amount = r._amount;
     }
     this._nMerged = this._nMerged ? this._nMerged + 1 : 2;
   },
 
   treeNameMatches: function(aTreeName) {
     // Nb: the '/' must be present, because we have a KIND_OTHER reporter
     // called "explicit" which is not part of the "explicit" tree.
-    return this._unsafePath.indexOf(aTreeName) === 0 &&
+    return this._unsafePath.startsWith(aTreeName) &&
            this._unsafePath.charAt(aTreeName.length) === '/';
   }
 };
 
 function getReportsByProcess(aMgr)
 {
   // Ignore the "smaps" multi-reporter in non-verbose mode, and the
   // "compartments" multi-reporter all the time.  (Note that reports from these
   // multi-reporters can reach here as single reports if they were in the child
   // process.)
 
   function ignoreSingle(aPath) 
   {
-    return (aPath.indexOf("smaps/") === 0 && !gVerbose) ||
-           (aPath.indexOf("compartments/") === 0)
+    return (aPath.startsWith("smaps/") && !gVerbose) ||
+           (aPath.startsWith("compartments/"))
   }
 
   function ignoreMulti(aName)
   {
     return ((aName === "smaps" && !gVerbose) ||
             (aName === "compartments"));
   }
 
@@ -1520,17 +1524,17 @@ Compartment.prototype = {
 function getCompartmentsByProcess(aMgr)
 {
   // Ignore anything that didn't come from the "compartments" multi-reporter.
   // (Note that some such reports can reach here as single reports if they were
   // in the child process.)
 
   function ignoreSingle(aPath) 
   {
-    return aPath.indexOf("compartments/") !== 0;
+    return !aPath.startsWith("compartments/");
   }
 
   function ignoreMulti(aName)
   {
     return aName !== "compartments";
   }
 
   let compartmentsByProcess = {};
@@ -1555,17 +1559,17 @@ function getCompartmentsByProcess(aMgr)
 
     } else if (unsafeNames[0] === "compartments" && unsafeNames[1] == "user" &&
         unsafeNames.length == 3)
     {
       isSystemCompartment = false;
       // These null principal compartments are user compartments according to
       // the JS engine, but they look odd being shown with content
       // compartments, so we put them in the system compartments list.
-      if (unsafeNames[2].indexOf("moz-nullprincipal:{") === 0) {
+      if (unsafeNames[2].startsWith("moz-nullprincipal:{")) {
         isSystemCompartment = true;
       }
 
     } else {
       assert(false, "bad compartments path: " + aUnsafePath);
     }
     let c = new Compartment(unsafeNames[2], isSystemCompartment);