Giter Club home page Giter Club logo

jsutils's Introduction

JSutils - Small, efficient JavaScript utilities

JSutils is a collection of JavaScript utilities that are not available in the standard library. It uses modern JavaScript and tries to balance between performance, readability and versatility.

Included utilities

Deque

A fast double-ended queue implemented on top of a growable ring buffer and relies on native JS memory operations like Array.concat and Array.copyWithin.

const queue = new Deque([1, 2, 3, 4])

queue.pushBack(queue.popFront())
queue.pushBack(queue.popFront())

queue.toArray() // [3, 4, 1, 2]

It also supports TypeScript and Generics

const queue = new Deque<number>()

// Error: Type 'number | null' is not assignable to type 'number'.
const x: number = queue.popBack()

BitSet

An efficient, dynamic bitset implementation. It's generally faster and smaller than the native generic Set thanks to the use of TypedArray and as the name suggest, bitwise operations.

const set = new BitSet([1, 5, 10, 500])
set.delete(10)
set.add(100)

set.items() // [1, 5, 100, 500]

Todo:

  • Ranged add/remove/flip
  • Set operations (e.g. difference, union, intersection, equality, subset/superset relations)

WorkerPool

Generic implementation of a worker thread pool in JavaScript for quick and easy parallelization. Internally uses a simpler implementation of Deque.

You can use this library to import any ES module and turns all exported functions into asynchronous, parallelized functions.

// fib.js
export function fib(n) {
  return n < 2 ? 1 : fib(n - 1) + fib(n - 2)
}
const pool = await WorkerPool.create("./fib.js")

import { fib } from "./fib.js"
const input = new Array(8).fill(40)

// ~1.2s on my machine
const result_par = await Promise.all(input.map(pool.fib))

// ~7.0s on my machine
const result_seq = input.map(fib)

WorkerPool.destroy(pool) // terminate workers when they are no longer needed

Heap

A low level set of functions to manipulate arrays like binary heaps. They can be used directly or for implementing priority queues. They're inspired by Python's heapq module. The default ordering is less. You can override the comparator to use them for max heaps or with custom objects.

import * as heap from "./heap.js"
const h = [56, 32, 68, 52, 20]
heap.heapify(h)
heap.push(h, heap.pop(h) * 2)
heap.push(h, heap.pop(h) * 2)
heap.push(h, heap.pop(h) * 2)
console.log(h) // [52, 56, 68, 64, 80]

const greater = (a, b) => a > b
heap.heapify(h, greater)
console.log(Array.from({length: 3}, () => heap.pop(h, greater))) // [80, 68, 64]

Installation

You can either:

  • Download the files to your project
  • Import with Github raw file URL (only for deno and browsers)

License

This project is licensed under the MIT License.

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.