<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var users = [
{ 'user': 'joey', 'age': 32 },
{ 'user': 'ross', 'age': 41 },
{ 'user': 'chandler', 'age': 39 }
]
users.sort((a,b) => a.user.localeCompare(b.user));
_.orderBy(users, 'user', 'asc')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
native sort of objects by linus | |
lodash _.orderBy by linus |
Test name | Executions per second |
---|---|
native sort of objects by linus | 3025909.8 Ops/sec |
lodash _.orderBy by linus | 685213.0 Ops/sec |
Let's break down the provided benchmark test cases.
Benchmark Definition JSON
The provided benchmark definition is a JSON object that describes the two benchmark tests:
localeCompare
method to compare two strings (in this case, user names). The localeCompare
method is used to determine the sorting order of objects based on their property values._orderBy
function, which sorts an array of objects based on a specified key and sorting order.Script Preparation Code
The script preparation code is a JavaScript snippet that creates an array of objects with user
properties:
var users = [
{ 'user': 'joey', 'age': 32 },
{ 'user': 'ross', 'age': 41 },
{ 'user': 'chandler', 'age': 39 }
];
This array will be used as the input for both benchmark tests.
HTML Preparation Code
The HTML preparation code includes a script tag that loads the Lodash library:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Lodash is a utility library that provides various functions for tasks such as string manipulation, array operations, and more.
Individual Test Cases
The two individual test cases are:
localeCompare
method to compare the user
properties of the input array:users.sort((a,b) => a.user.localeCompare(b.user));
_orderBy
function to sort the input array based on the user
property:_.orderBy(users, 'user', 'asc');
Options Compared
The two tests compare the performance of:
localeCompare
method for sorting objects by a string property._orderBy
function for sorting an array of objects based on a specified key.Pros and Cons
Here are some pros and cons of each approach:
locale
setting).Library and Syntax
The Lodash library is a utility library that provides various functions for tasks such as string manipulation, array operations, and more. In this benchmark, the _orderBy
function is used to sort an array of objects based on a specified key.
There are no special JavaScript features or syntaxes being tested in this benchmark.