Giter VIP home page Giter VIP logo

stljs's Introduction

STL.js

Standard Template Library for {Type,Java}Script projects

Why?

Have you ever been in a situation where you need to use a deque but you don't have such a thing in JavaScript? "Why don't you use an array?", they might ask. I'd like to give an answer: time complexity

Let's take a look at the deque example. It has O(1) for {push,pop}_{front,back} (C++ API) operations, in a word, it's a doubly linked list and you can push and pop items to boths ends fast. You can't take an arbitrary element by index as you can do with arrays, and that's the price you pay. That's fine since you don't need to, doubly linked lists serve the different purpose.

What's about unshift complexity of Array in V8? Benchmarks show it's significantly slower than push. It moves the elements to relayout memory, as you might expect, it's O(n) in worst case.

Deque

Compliant with JavaScript Array API.

Benchmarks ๐Ÿš€

Array#unshift works in O(n) time and Deque#unshift works in O(1) time, check out the benchmarks:

Name Total iterations Iterations per second
deque#unshift 46,938 10,672
array#unshift 325 74

Array#shift is optimized though, works faster than Deque#shift on ~20%:

Name Total iterations Iterations per second
array#shift 2,785,723,217 633,983,435
deque#shift 2,263,189,082 515,650,280

Array#push and Array#pop works faster as expected:

Name Total iterations Iterations per second
array#push 55,567 13,214
deque#push 23,356 5,278
Name Total iterations Iterations per second
array#pop 1,712,950,723 1,334,438,329
deque#pop 388,688,614 301,159,631

Note: tests run ~4s on 10.000 items

API

push

import { Deque } from 'stljs';
const deque = new Deque<number>()
deque.push(1);
// 1
deque.back();

pop

import { Deque } from 'stljs';
const deque = new Deque<number>()
deque.push(1);
// 1
deque.pop();

unshift

import { Deque } from 'stljs';
const deque = new Deque<number>()
deque.push(2);
deque.push(3);
deque.unshift(1);
// 1
deque.front();

shift

import { Deque } from 'stljs';
const deque = new Deque<number>()
deque.push(1);
deque.push(2);
deque.push(3);
// 1
deque.shift();

length

import { Deque } from 'stljs';
const deque = new Deque<number>()
deque.push(1);
deque.push(2);
deque.push(3);

while (deque.length > 0) {
  // 3, 2, 1
  deque.pop();
}

stljs's People

Contributors

dependabot[bot] avatar vitkarpov avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

icodein

stljs's Issues

Implement heaps

Implement make_heap, push_heap & pop_heap.
Details: https://www.geeksforgeeks.org/heap-using-stl-c/

Example:

const arr = [1,2,3,4];

makeHeap(arr);
pushHeap(9);

// shows 9 since it's the biggest element in the heap
console.log(arr[0]);

popHeap(arr);

// shows 4 since 9 has been removed, and 4 is the next biggest element in the heap
console.log(arr[0]);

Set up a CI

There are unit tests & benchmarks. It's time to set up a CI.

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.