Coder Social home page Coder Social logo

fast-sort's Introduction

fast-sort

Build Status Code quality Codacy Badge Open Source Love MIT Licence

NPM Package

Blazing fast array sorting that outperforms lodash sorting by ~2x (in some cases it's more then 5x). Take a look at the benchmark section for more information about performance.

Fast sort highlights

  • Sorting an array of objects by one or more properties
  • Sorting flat arrays
  • Sorting in multiple directions
  • Easy to read syntax for asc and desc sorting
  • Faster then other sort alternatives
  • Undefined and null values are always sorted to bottom of list no matter if ordering is ascending or descending.

Under the hood sort use a native JavaScript sort. Usage of native sort implies that sorting is not necessarily stable and it also implies that input array is modified(sorted) same as it would be when applying native sort

Example

  import sort from 'fast-sort';

  sort([1,4,2]).asc(); // => [1, 2, 4]
  sort([1,4,2]).desc(); // => [4, 2, 1]

  // Sort users [Object] ascending by firstName
  sort(users).asc(u => u.firstName);

  // If sorting by single property we can use string syntax
  // NOTE: sorting by string is available from version [1.3.0]
  sort(users).asc('firstName');

  // For sorting by nested property we have to provide sort function
  sort(users).desc(u => u.address.city);

  // Sort users by firstName, lastName and city
  sort(users).desc([
    'firstName',
    'lastName',
    u => u.address.city // String syntax is not available for nested properties
  ]);

  // Sort in multiple directions
  // NOTE: Available from version [1.5.0]
  sort(persons).by([
    { asc: 'name' }
    { desc: 'age' }
  ]);

  // Sort by any custom logic e.g sort vip users first
  sort(users).asc(u => u.tags === 'vip' ? 1 : -1);

  // Sorting values that are not sortable will return same value back
  sort(null).asc(); // => null
  sort(33).desc(); // => 33

NOTE: fast-sort is part of js-flock library exported as single module.

Benchmark

Benchmarking sort is not an easy task as there is so many different scenarios that can happen while sorting. Because of that 5 different benchmarks have been created to test how fast-sort is behaving on different inputs and sort scenarios. Each benchmark is run with different array sizes from small 100 items to large 100 000 items.

Every run of benchmark outputs different results but the results are constantly better then lodash sort and in following benchmark score ranges from 1.37x to 13.51x faster then lodash sort. This will vary on each benchmark run but it should not vary too much.

Benchmark scores

Benchmark has been run on:

  • 16 GB Ram
  • Intel® Core™ i5-4570 CPU @ 3.20GHz × 4
  • Ubuntu 16.04
  • Node 8.9.1

benchmark results

Running benchmark

To run benchmark on your PC follow steps from below

  1. git clone https://github.com/snovakovic/js-flock.git
  2. cd js-flock
  3. npm install
  4. npm run benchmark:sort

In case you notice any irregularities in benchmark or you want to add sort library to benchmark score please open issue here

fast-sort's People

Contributors

snovakovic avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.