search-optimized
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

search-optimized

Optimized regular repeated array searches

npm version npm bundle size NPM

Install

npm i search-optimized

Usage

In js/ts we constantly use Array.prototype.find() which is a linear search. Many times we need to find some data from an array. When the application scales and the list starts to grow, we will start seeing some lag.

These search optimizers can reduce the search times drastically if you need to find multiple objects from the list over time. Under the hood, it user Binary Search or a dictionary to reduce the repeated search time.

//sample interface for data
interface Student{
    id: number;
    name: string;
    class: string;
    section: string;
    ...
}

Binary Search(TS)

import SearchOptimiserBinary from 'search-optimized';

const arr: Array<Student> = getStudentDataArray(); // data that require regular searching
const comparator = (a: Student, b: Student)=> return a.id - b.id;   // [< 0: a < b]; [0: a == b]; [>0: a < b] 

const arrSearchOptimiser = new SearchOptimiserBinary<Student>(arr, comparator);  // initialization

// searching
const result: Student = arrSearchOptimiser.find({id: 12}); // argument is a value that is compatable with the comparator function 

Object Optimized Search(TS)

import SearchOptimiserDict from 'search-optimized';

const arr: Array<Student> = getStudentDataArray(); // data that require regular searching
const keyName = 'id'

const arrSearchOptimiser = new SearchOptimiserDict<Student>(arr, keyName);  // initialization

// searching
const result: Student = arrSearchOptimiser.find({id: 12}); // argument is a object that contains keyName

Binary Search(JS)

const { SearchOptimiserBinary } = require( 'search-optimized' );

const arr = getStudentDataArray(); // data that require regular searching
const comparator = (a, b)=> return a.id - b.id;   // [< 0: a < b]; [0: a == b]; [>0: a < b] 

const arrSearchOptimiser = new SearchOptimiserBinary(arr, comparator);  // initialization

// searching
const result = arrSearchOptimiser.find({id: 12});

Object Optimized Search(JS)

const { SearchOptimiserDict } = require( 'search-optimized' );

const arr = getStudentDataArray(); // data that require regular searching
const keyName = 'id'

const arrSearchOptimiser = new SearchOptimiserDict(arr, keyName);  // initialization

// searching
const result = arrSearchOptimiser.find({id: 12}); // argument is a object that contains keyName

License

MIT © Aditya Kumar

Package Sidebar

Install

npm i search-optimized

Weekly Downloads

1

Version

1.0.4

License

MIT

Unpacked Size

19.9 kB

Total Files

10

Last publish

Collaborators

  • kumar_93