Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One major bottleneck in your code is that you're recomputing nameA/nameB on every sort operation. Considering that each element in array may take part in several comparisons, that's a lot of unnecessary work. You should first build sorting index using same algorithm and then sort with it instead. You can also store sort key in same object you sort so you can just check if it is here and don't recompute it if it is.</p> <p>Another two things in your code are absolutely unnecessary: <code>.toLowerCase</code> on comparison with empty string and generation of useless <code>Data</code> objects, when <code>Data.parse</code> already specified explicitly as working with strings.</p> <p>I've made two versions of improved sort - one with helper function (prettier, more correct ideologically) and inline - may give some extra speed-up on some JS engines. They both run roughly 3000 times faster than your original: <a href="http://jsperf.com/custom-date-sort" rel="nofollow noreferrer">http://jsperf.com/custom-date-sort</a>.</p> <p>(Oh, and helper function happens to be suitable for your underscore sortBy question).</p> <p>With helper:</p> <pre><code>function fill_datetime_ms(obj){ if (obj.date == "" || obj.time == ""){ return obj.dateTimeMs = "" } else { return obj.dateTimeMs = Date.parse(obj.date.toLowerCase() + " " + obj.time) } } var sorted = results.sort(function (a, b) { var nameA = a.dateTimeMs var nameB = b.dateTimeMs if (!nameA &amp;&amp; nameA !== "" &amp;&amp; !nameA !== 0){ nameA = fill_datetime_ms(a) } if (!nameB &amp;&amp; nameB !== "" &amp;&amp; !nameB !== 0){ nameB = fill_datetime_ms(b) } return nameA === nameB ? 0 : nameA &lt; nameB ? -1 : 1; }); </code></pre> <p>Inline:</p> <pre><code>var sorted = results.sort(function (a, b) { var nameA = a.dateTimeMs var nameB = b.dateTimeMs if (!nameA &amp;&amp; nameA !== "" &amp;&amp; !nameA !== 0){ if (a.date == "" || a.time == ""){ nameA = a.dateTimeMs = "" } else { nameA = a.dateTimeMs = Date.parse(a.date.toLowerCase() + " " + a.time) } } if (!nameB &amp;&amp; nameB !== "" &amp;&amp; !nameB !== 0){ if (b.date == "" || b.time == ""){ nameB = b.dateTimeMs = "" } else { nameB = b.dateTimeMs = Date.parse(b.date.toLowerCase() + " " + b.time) } } return nameA === nameB ? 0 : nameA &lt; nameB ? -1 : 1; }); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload