Bug 1304638 - Return early from sorting if the array is single-element or empty. r?nbp draft
authorEmilio Cobos Álvarez <ecoal95@gmail.com>
Sun, 09 Oct 2016 15:57:21 +0200
changeset 422994 10409beaa604513db385c8d1f408b5c3a67e746e
parent 422989 ebc452cc36cb7d12a20f6c09379fdb5b93740287
child 423230 ca7229c4c97390d7ae447177ca6c798ce7ea2366
child 423232 4bb28a105522c936f0f9dda2c6820b0159f1e4ea
child 423261 0e193dcafffdf51970c69e91d80eda570b519ddb
push id31793
push userbmo:ecoal95@gmail.com
push dateSun, 09 Oct 2016 14:37:36 +0000
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
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;