All Projects → Pimm → Mapsort

Pimm / Mapsort

Licence: mit
Performant sorting for complex input

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Mapsort

Gods
GoDS (Go Data Structures). Containers (Sets, Lists, Stacks, Maps, Trees), Sets (HashSet, TreeSet, LinkedHashSet), Lists (ArrayList, SinglyLinkedList, DoublyLinkedList), Stacks (LinkedListStack, ArrayStack), Maps (HashMap, TreeMap, HashBidiMap, TreeBidiMap, LinkedHashMap), Trees (RedBlackTree, AVLTree, BTree, BinaryHeap), Comparators, Iterators, …
Stars: ✭ 10,883 (+29313.51%)
Mutual labels:  sort, map
Phunctional
⚡️ λ PHP functional library focused on simplicity and performance
Stars: ✭ 243 (+556.76%)
Mutual labels:  performance, map
Imtools
Fast and memory-efficient immutable collections and helper data structures
Stars: ✭ 85 (+129.73%)
Mutual labels:  performance, map
AhaAlgorithms
《啊哈算法》书上代码
Stars: ✭ 47 (+27.03%)
Mutual labels:  map, sort
Pizza Delivery
university project : Android pizza delivery app
Stars: ✭ 32 (-13.51%)
Mutual labels:  map
Mapsforge
Vector map library and writer - running on Android and Desktop.
Stars: ✭ 885 (+2291.89%)
Mutual labels:  map
Fastglobal
Fast no copy globals for Elixir & Erlang.
Stars: ✭ 882 (+2283.78%)
Mutual labels:  performance
Clusterize.js
Tiny vanilla JS plugin to display large data sets easily
Stars: ✭ 6,995 (+18805.41%)
Mutual labels:  performance
Computesharp
A .NET 5 library to run C# code in parallel on the GPU through DX12 and dynamically generated HLSL compute shaders, with the goal of making GPU computing easy to use for all .NET developers! 🚀
Stars: ✭ 982 (+2554.05%)
Mutual labels:  performance
Flutter native map
Stars: ✭ 35 (-5.41%)
Mutual labels:  map
Monomorphist
monomorphist - a JavaScript performance companion
Stars: ✭ 30 (-18.92%)
Mutual labels:  performance
Openshift Psap
Example roles and yaml files for performance-sensitive applications running on OpenShift
Stars: ✭ 20 (-45.95%)
Mutual labels:  performance
Vue World Map
A Vue JS component for displaying dynamic data on a world map.
Stars: ✭ 33 (-10.81%)
Mutual labels:  map
Defer Render Hoc
defer expensive react renders until the next two rAF's
Stars: ✭ 15 (-59.46%)
Mutual labels:  performance
Jquery Mapael
jQuery plugin based on raphael.js that allows you to display dynamic vector maps
Stars: ✭ 981 (+2551.35%)
Mutual labels:  map
Sysbench Docker Hpe
Sysbench Dockerfiles and Scripts for VM and Container benchmarking MySQL
Stars: ✭ 14 (-62.16%)
Mutual labels:  performance
Highcharts China Geo
Highcharts 中国地图,Highcharts 中国省市地图,Highcharts China Map,南海诸岛geo
Stars: ✭ 29 (-21.62%)
Mutual labels:  map
Mapbox Gl Js
Interactive, thoroughly customizable maps in the browser, powered by vector tiles and WebGL
Stars: ✭ 8,017 (+21567.57%)
Mutual labels:  map
Netsniff Ng
A Swiss army knife for your daily Linux network plumbing.
Stars: ✭ 915 (+2372.97%)
Mutual labels:  performance
Open Redistricting
A collaborative, open legislative redistricting tool, built atop GitHub.
Stars: ✭ 21 (-43.24%)
Mutual labels:  map

mapsort · License (X11/MIT) npm version Build 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.4', '1.62', '3.35']

Sorting them with no compare function would place '12.4' before '3.35', 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:

mapSort(
	['12.4', '1.62', '3.35'],
	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/[email protected]^1.0.4"></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

  • 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-2021 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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].