Effective use of array sort in JavaScript

Infor Process Automation uses the Mozilla Rhino JavaScript engine to run process flows. A new Mozilla Rhino version is released and adopted by IPA, which results in a change to the performance of the Array.prototype.sort() function.

Based on the sorting test, the newer version of the sorting algorithm is slower than in the previous versions. If you use sort() extensively in your script, especially with large arrays, you can experience longer run times.

To sort an array of objects by multiple criteria, you must use a single sort() call with a comprehensive comparison function instead of a multiple sort() call. For example, you can sort by name, then by account, then by year, all within one function. This approach reduces the work unit run time.

This list shows the benefits of using a single sort() call:

  • A single sort() call processes the full array simultaneously. The comparison logic of this method is more complex but more efficient than re-sorting the entire dataset.
  • A single sort() call puts all your sorting logic into one function, making the code easier to read and to maintain, and less prone to errors.
  • A single sort() call works in all JavaScript environments. The multiple sort relies on a stable sort() method, preserving the order of equal elements, which is not guaranteed in all JavaScript versions.