GithubHelp home page GithubHelp logo

justjavac / deno-murmurhash Goto Github PK

View Code? Open in Web Editor NEW
3.0 3.0 0.0 10 KB

An incremental implementation of MurmurHash3 for JavaScript

License: MIT License

TypeScript 100.00%
deno deno-module deno-mod deno-modules

deno-murmurhash's Introduction

deno-murmurhash

tag Build Status license

An incremental implementation of MurmurHash3 for JavaScript

This version works significantly faster than the non-incremental version if you need to hash many small strings into a single hash, since string concatenation (to build the single string to pass the non-incremental version) is fairly costly.

In one case tested, using the incremental version was about 50% faster than concatenating 5-10 strings and then hashing.

Usage

import Murmurhash3 from "https://deno.land/x/murmurhash/mod.ts";

const hash = MurmurHash3("string");

// Incrementally add text
hash.hash("more strings");
hash.hash("even more strings");

// All calls can be chained if desired
hash
  .hash("and")
  .hash("some")
  .hash("more");

// Get a result
hash.result();
// returns 0xe4ccfe6b

API

MurmurHash3(key?: string, seed?: number)

Get a hash state object, optionally initialized with the given key and seed. seed must be a positive integer if provided.

const hashState = new MurmurHash3();

MurmurHash3.prototype.hash(key: string)

Incrementally add key to the hash. This can be called as many times as you want for the hash state object, including after a call to result().

MurmurHash3.prototype.result(): number

Get the result of the hash as a 32-bit positive integer. This performs the tail and finalizer portions of the algorithm, but does not store the result in the state object.

This means that it is perfectly safe to get results and then continue adding strings via hash.

// Do the whole string at once
new MurmurHash3("this is a test string").result();
// 0x70529328

// Do part of the string, get a result, then the other part
const m = MurmurHash3("this is a");
m.result();
// 0xbfc4f834
m.hash(" test string").result();
// 0x70529328 (same as above)

MurmurHash3.prototype.reset(seed: number)

Reset the state object for reuse, optionally using the given seed (defaultsto 0 like the constructor).

Thanks

Heavily inspired by garycourt/murmurhash-js, kazuyukitanimura/murmurhash-js, jensyt/imurmurhash-js.

deno-murmurhash's People

Contributors

justjavac avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.