Git Product home page Git Product logo

asorted's Introduction

Asorted

Fast sorted array in Javascript. Uses binary search for addition and deletion.

Installation

node.js npm install asorted

Examples

var Asorted = require("asorted");
var sorted = new Asorted(4, 42, 8, 16, 15, 23);
console.log(sorted.array);                      // [ 4, 8, 15, 16, 23, 42 ]
sorted.search(16);                              // 3
sorted.search(3);                               // -1
sorted.remove(16);                              // [ 4, 8, 15, 23, 42 ]
sorted.insert(16);                              // [ 4, 8, 15, 16, 23, 42 ]
sorted.insert(50000);                           // [ 4, 8, 15, 16, 23, 42, 50000 ]

var sorted2 = new Asorted({ sortBy: 'size'}, { name: 'Danny', size: 10 }, { name: 'Linux', size: 9 } );
//[ { name: 'Linus', size: 9 }, { name: 'Danny', size: 10 } ]
sorted2.insert({ name: 'Jeremey', size: 20 });
//[ { name: 'Linux', size: 9 },
//  { name: 'Danny', size: 10 },
//  { name: 'Jeremey', size: 20 } ]

Geeky stuff

Since binary search is used for adding and deleting, these operations should be in O(log n). This however is just for finding the appropriate index. The splice operation is dependent on the browsers implementation. Modern browsers heavily optimize these operations but the since this uses a dynamic array the worst case could be O(n).

Roadmap

  • Implemenet sorted arrays of objects. When working with flux.js stores, I normally use native arrays for lists. Coming from backbone.js which has collections which are basically fancy arrays I need a replacement which can keep an array of objects sorted based on a given key. So that's the plan. Pull requests are welcome.
  • Get this on bower, npm and whatever-package-manger-is-now-cool
  • Unit tests.... Yes it's coming :)

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.