Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36
Chrome 81
Mac OS X 10.15.3
Desktop
4 years ago
Test name Executions per second
_.sortBy 14.0 Ops/sec
array.prototype.sort localeCompare 83.3 Ops/sec
array.prototype.sort non-locale 375.9 Ops/sec
HTML Preparation code:
x
 
1
2
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
 
function getRandomInt(max) {
  return Math.floor(Math.random() * Math.floor(max));
}
var arr = [];
for(var i = 0; i < 100000; i++){
  arr.push({value:getRandomInt(100)+""});
}
Tests:
  • _.sortBy

     
    _.sortBy(arr,"value");
  • array.prototype.sort localeCompare

     
    arr.sort((a,b) => a.value.localeCompare(b.value));
  • array.prototype.sort non-locale

     
    arr.sort((a, b) => (a.value < b.value ? -1 : (a.value > b.value ? 1 : 0)))