Bug 1304638 - Return early from sorting if the array is single-element or empty. r=nbp
authorEmilio Cobos Álvarez <ecoal95@gmail.com>
Sun, 09 Oct 2016 15:57:21 +0200
changeset 319837 107238b89fccb625ed3eedcb6854e7e91b5609a1
parent 319836 27f76213fa87e8469387d9783afcb7c3e404eb0f
child 319838 a8bb58ecb17df251c552f8e01b2d599abc64d127
push id20748
push userphilringnalda@gmail.com
push dateFri, 28 Oct 2016 03:39:55 +0000
treeherderfx-team@715360440695 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnbp
bugs1304638
milestone52.0a1
Bug 1304638 - Return early from sorting if the array is single-element or empty. r=nbp A single-element array is always sorted, by definition. MozReview-Commit-ID: 9GXFGqomM9x Signed-off-by: Emilio Cobos Álvarez <ecoal95@gmail.com>
js/src/builtin/Array.js
--- a/js/src/builtin/Array.js
+++ b/js/src/builtin/Array.js
@@ -198,16 +198,19 @@ function ArrayStaticSome(list, callbackf
 /* ES6 draft 2016-1-15 22.1.3.25 Array.prototype.sort (comparefn) */
 function ArraySort(comparefn) {
     /* Step 1. */
     var O = ToObject(this);
 
     /* Step 2. */
     var len = ToLength(O.length);
 
+    if (len <= 1)
+      return this;
+
     /* 22.1.3.25.1 Runtime Semantics: SortCompare( x, y ) */
     var wrappedCompareFn = comparefn;
     comparefn = function(x, y) {
         /* Steps 1-3. */
         if (x === undefined) {
             if (y === undefined)
                 return 0;
            return 1;