Giter VIP home page Giter VIP logo

xxhash's Introduction

js-xxHash

Pure Javascript / Typescript Implementation of xxHash

This is an implementation for the XXH32 Algorithm A 64-bit version might come a bit later.

Why another version

  • I needed a fast simple hash for short to medium sized strings.
  • It needed to be pure JS.

Installation

npm install --save js-xxhash

Usage

Pure JS

Internally it uses TextEncoder to convert strings to a UTF-8 Uint8Arrays.

import { xxHash32 } from 'js-xxhash';

let seed = 0;
let str = 'My text to hash 😊';
let hashNum = xxHash32(str, seed);
console.log(hashNum.toString(16));

Expected:

af7fd356

Node JS

import { xxHash32 } from 'js-xxhash';

let seed = 0;
let str = 'My text to hash 😊';
let hashNum = xxHash32(str, seed);
console.log(hashNum.toString(16));

Browser

// Using a bundler
import { xxHash32 } from 'js-xxhash';
// Using a CDN like jsDelivr
import { xxHash32 } from 'https://cdn.jsdelivr.net/npm/js-xxhash@{version}/index.mjs';

let seed = 0;
let str = 'My text to hash 😊';
let hashNum = xxHash32(str, seed);
console.log(hashNum.toString(16));

Performance

To evaluate performance this package was compared to:

One average a lorem-ipsum "word" is between 5 and 6 characters.

Performance for Strings

Running Perf Suite: xxhash-string-words-10
Evaluate xxhash performance with a string of 10 words.
✔ js-xxhash string       731_148.14 ops/sec
✔ xxhashjs string        432_753.87 ops/sec
✔ xxhash-wasm string   3_381_907.91 ops/sec

Running Perf Suite: xxhash-string-words-100
Evaluate xxhash performance with a string of 100 words.
✔ js-xxhash string       420_458.19 ops/sec
✔ xxhashjs string        124_443.56 ops/sec
✔ xxhash-wasm string   2_289_457.63 ops/sec

Running Perf Suite: xxhash-string-words-1000
Evaluate xxhash performance with a string of 1000 words.
✔ js-xxhash string        74_861.33 ops/sec
✔ xxhashjs string         16_656.57 ops/sec
✔ xxhash-wasm string     729_339.20 ops/sec

Running Perf Suite: xxhash-string-words-10000
Evaluate xxhash performance with a string of 10000 words.
✔ js-xxhash string         6_293.40 ops/sec
✔ xxhashjs string            551.90 ops/sec
✔ xxhash-wasm string      90_170.30 ops/sec

Running Perf Suite: xxhash-string-words-100000
Evaluate xxhash performance with a string of 100000 words.
✔ js-xxhash string           709.30 ops/sec
✔ xxhashjs string             40.05 ops/sec
✔ xxhash-wasm string       8_093.17 ops/sec

Performance with a Uint8Array Buffer

Running Perf Suite: xxhash-buffer-words-10
Evaluate xxhash performance with a buffer containing a string of 10 words.
✔ js-xxhash buffer       2_859_850.03 ops/sec
✔ xxhashjs buffer          699_053.22 ops/sec
✔ xxhash-wasm buffer     3_657_504.67 ops/sec

Running Perf Suite: xxhash-buffer-words-100
Evaluate xxhash performance with a buffer containing a string of 100 words.
✔ js-xxhash buffer         800_609.77 ops/sec
✔ xxhashjs buffer          402_424.91 ops/sec
✔ xxhash-wasm buffer     2_569_294.66 ops/sec

Running Perf Suite: xxhash-buffer-words-1000
Evaluate xxhash performance with a buffer containing a string of 1000 words.
✔ js-xxhash buffer         79_925.04 ops/sec
✔ xxhashjs buffer          55_568.13 ops/sec
✔ xxhash-wasm buffer      753_856.33 ops/sec

Running Perf Suite: xxhash-buffer-words-10000
Evaluate xxhash performance with a buffer containing a string of 10000 words.
✔ js-xxhash buffer          8_152.57 ops/sec
✔ xxhashjs buffer           6_046.82 ops/sec
✔ xxhash-wasm buffer      104_463.50 ops/sec

Running Perf Suite: xxhash-buffer-words-100000
Evaluate xxhash performance with a buffer containing a string of 100000 words.
✔ js-xxhash buffer            458.33 ops/sec
✔ xxhashjs buffer             602.90 ops/sec
✔ xxhash-wasm buffer        9_835.61 ops/sec

xxhash's People

Contributors

dependabot[bot] avatar jason3s avatar lmammino avatar github-actions[bot] avatar

Stargazers

M̴̧̡̡̢͎̬͖̬̠̭̱̝̩̪̝̠͎͈̪̰͕͓̼̻͎̮̜̫̬͍̱̭̠̟̖͉͈̺͙͉͒́͂̾̅̚̚͜͜͜Ę̷̡̡̧̨͎͕̟̪̰͍̬̮̥̝̖̯͕̳̳̜̭͖̯͙̜̰̤̼̠̼̪̝̐̔̍̋͜͝R̶̘̹̠̻̺̟̞̦͗̓̓̐̄̀͊̏̔͂͑̉͆͘̕͝T̴̢̨̛̩̮̖̺̱͙̳̪̠̼͐͌̇̀̓́͂̎̔͗͂̄̈́̆́̏̉̀͑͗͐̈́̋̇͌̿̅͒̓͒͒̂͋͘͝͝ avatar Marco Beierer avatar Alex Bacart avatar Nikola Hristov avatar Okiki Ojo avatar tomixy avatar Yuji Sugiura avatar Christopher Cortes avatar PatrickJS avatar Marcus S. Abildskov avatar Hyeseong Kim avatar Anton Petrov avatar Prana Adiwira avatar trashman avatar  avatar Alexis H. Munsayac avatar H11 avatar Jeff Carpenter avatar Denis Denisov avatar Isaac Karth avatar Yann Collet avatar Gabriel Montes avatar tai-kun avatar Philipp Krüger avatar Kelvin Jayanoris avatar Aviv Haruzi avatar Chee Aun avatar Prathak Paisanwatcharakit avatar YuK_Ota avatar Gnlow avatar Kristóf Poduszló avatar Moein Alizadeh avatar LEI avatar

Watchers

James Cloos avatar  avatar  avatar

xxhash's Issues

Error: Cannot find module './xxHash32.js'

after installing js-xxhash I get the following error when importing it:

$ node -e "require('js-xxhash');"
node:internal/modules/cjs/loader:1147
  throw err;
  ^

Error: Cannot find module './xxHash32.js'
Require stack:
- /private/tmp/benchmark/node_modules/js-xxhash/dist/cjs/index.cjs
- /private/tmp/benchmark/[eval]
    at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
    at Module._load (node:internal/modules/cjs/loader:985:27)
    at Module.require (node:internal/modules/cjs/loader:1235:19)
    at require (node:internal/modules/helpers:176:18)
    at Object.<anonymous> (/private/tmp/benchmark/node_modules/js-xxhash/dist/cjs/index.cjs:4:22)
    at Module._compile (node:internal/modules/cjs/loader:1376:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
    at Module.load (node:internal/modules/cjs/loader:1207:32)
    at Module._load (node:internal/modules/cjs/loader:1023:12)
    at Module.require (node:internal/modules/cjs/loader:1235:19) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/private/tmp/benchmark/node_modules/js-xxhash/dist/cjs/index.cjs',
    '/private/tmp/benchmark/[eval]'
  ]
}

Node.js v20.10.0

also this library requires tslib as dependency, but it's not defined as a dependency. either inline the required tslib stuff or mark it as dependency.

I assume ts2mjs dist/cjs --cjs is broken

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.