Skip to content

Pimm/mapsort

Repository files navigation

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

Performant sorting for complex input.

Preface

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.

Concept

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

['12', '1', '3.14']

Sorting them with no compare function would place '12' before '3.14', so we need such a function:

['12', '1', '3.14'].toSorted((a, b) => parseFloat(a) - parseFloat(b));

This works!

The drawback is that parseFloat is called twice every time our compare function is used. Sorting three numbers results in 6 (or 4) parseFloat calls, of which only 3 are strictly necessary.

3 or 6 parseFloat calls makes no difference. However, next time we might be sorting names. Lucia Ávila would like to appear amidst the other As, thus we have to correctly handle diacritics. Amelie de Wit would like to appear amidst the other Ws, thus 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:

mapSort(['12', '1', '3.14'], parseFloat, (a, b) => a - b);

Installation

Install mapsort using npm or Yarn and import the function:

import mapSort from 'mapsort';

Alternatively, include mapsort through unpkg:

<script src="https://unpkg.com/mapsort@^1.0.10"></script>

This alternative makes the function available at window.mapSort.

Usage

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

Notes

  • This library does not sort in-place, making it more similar to [].toSorted than [].sort.
  • This library probably performs stable sorting.

License (X11/MIT)

Copyright (c) 2019-2023 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.