npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mapsort

v1.0.10

Published

Performant sorting for complex input

Downloads

2,713

Readme

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.