Skip to content
Performant sorting for complex input
JavaScript TypeScript HTML
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
benchmarks Added some documentation for the conceptual benchmark findings. Jun 11, 2019
documentation Added documentation about stability. Jul 30, 2019
source Added documentation about stability. Jul 30, 2019
test Made the third argument of the map callback more correctly typed. Jul 7, 2019
.babelrc Added the implementation and one test. The implementation works accor… Apr 10, 2019
.gitignore .gitignore cleanup. Jun 17, 2019
.prettierrc.json Added some files to make this lib "score" better. Jun 19, 2019
.travis.yml Added Travis bulid configuration file, which tests but also sends cov… Jun 19, 2019
babel.config.json Compiling to CommonJS and ES modules, as well as an IIFE. Jun 11, 2019
copying.txt Added license (X11/MIT) Jun 17, 2019
index.d.ts Added documentation about stability. Jul 30, 2019
package-lock.json Added the author and repository to package.json. Jun 17, 2019
package.json Added a homepage (to package.json). Jun 20, 2019 Added documentation about stability. Jul 30, 2019 Added documentation about stability. Jul 30, 2019
rollup.config.js Simplified the rollup configuration. Aug 3, 2019

mapsort · License (X11/MIT) npm version Build status Coverage status

Performant sorting for complex input.


You do not need this library unless you are having performance issues. mapsort does not add any functionality not present in plain JavaScript. Rather, it greatly improves your performance in case:

  • sorting is your bottleneck, and
  • the elements in your arrays require expensive preprocessing before their correct order can be determined.


Imagine we are sorting this array of numbers, represented as strings:

['12.4', '1.62', '3.35']

Sorting them with no compare function would place '3.35' after '12.4', so we need a such a function:

['12.4', '1.62', '3.35'].sort((a, b) => parseFloat(a) - parseFloat(b));

This works!

The only drawback is that parseFloat is called twice every time our compare function is used, resulting in 6 parseFloat calls in this example (4 if the original order were optimal).

A dozen parseFloat calls is fine. However, next time we might be sorting names. Lucia Ávila would like to appear amidst the other As, and we have to correctly handle diacritics. Amelie de Wit would like to appear amidst the other Ws, and we have to detect tussenvoegsels. And the number of calls to the compare function grows loglinearly with the number of names. As our preprocessing becomes more expensive and our arrays become longer, this could produce perceivable hiccups.

mapsort reduces the number of times an element is preprocessed to 1:

	['12.4', '1.62', '3.35'],
	(a, b) => a - b


Install mapsort using npm or Yarn and import the function:

import mapSort from 'mapsort';

Alternatively, include mapsort through unpkg:

<script src="^1.0.1"></script>

This alternative makes the function available at window.mapSort.


const sortedArray = mapSort(
	element => {
		// Return the version of "element" which is ideal for
		// sorting. This version is passed to the compare
		// function below.
	(a, b) => {
		// Return a negative number if a comes before b;
		// a positive number if b comes before a; or 0 if they
		// are equal.


  • Contrary to [].sort, this library does not sort in-place. It returns a new, sorted array. The original array is left untouched.
  • This library maps each element of your array to a "sortable" version but returns a sorted array containing the originals. I.e. in the example above ['1.62', '3.35', '12.4'] is returned; not [1.62, 3.35, 12.4].
  • This library probably performs stable sorting.

License (X11/MIT)

Copyright (c) 2019 Pimm "de Chinchilla" Hogeling, Edo Rivai

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

The Software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. in no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the Software or the use or other dealings in the Software.

You can’t perform that action at this time.