Giter Site home page Giter Site logo

array-push-at-sort-position's Introduction

array-push-at-sort-position

Push items to an array at their correct sort-position which is much faster then re-sorting the array.

Adding an item to an array with push() and sort() has O(n*log(n)) while inserting the item at the correct sort-position has O(n).

  npm install array-push-at-sort-position --save
// instead of pushing and resorting like this:
const arrayWithItems = arrayWithItems.slice();
arrayWithItems.push(newItem);
const arrayWithNewItems = arrayWithItems.sort(sortComparator);

// you can push the newItem directly into the correct sorting position
const insertPosition = pushAtSortPosition(
  arrayWithItems,
  newItem,
  sortComparator,
  /**
   * Start lowest index
   * Use 0 by default. If you use this method to merge sorted arrays, you might
   * use a  higher value if you know that the newItem will not be positioned before that index.
   */
  0
);

Important

  • Calling pushAtSortPosition will not copy the array. It will mutate the input array. Call array.slice(0) on the input if you do not want the original array to be mutated.

See also

I tested many implementations and refactored the best ones. Some other modules:

array-push-at-sort-position's People

Contributors

gobengo avatar pubkey avatar renovate-bot avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

gobengo irtyamine

array-push-at-sort-position's Issues

Dependency Dashboard

This issue provides visibility into Renovate updates and their statuses. Learn more

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.


  • Check this box to trigger a request for Renovate to run again on this repository

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.