Git Product home page Git Product logo

sha1-wasm's Introduction

sha1-wasm

It's the Rust sha1 crate compiled to WebAssembly and bundled into a Node module.

Usage

In Node.js

const digest = require('./sha1');
digest("Hi there!"); // '95e2b07e12754e52c37cfd485544d4f444597bff'

In web

Instead of importing the root level of the package, import 'sha1/web' which loads the module from an array literal. This uses triple the byte size, but then it's not intended to be the primary use case.

const sha1 = require('./sha1/web');
sha1.then(digest => {
  /* do your thing; I don't know how to load modules asynchronously,
     and I'm glad I don't need to
   */);

Building

You need Rust nightly installed with the wasm32-unknown-unknown target.

curl https://sh.rustup.rs -sSf | sh

rustup toolchain install nightly

rustup target add wasm32-unknown-unknown

You also need wasm-gc installed.

cargo install wasm-gc

From the project root, run the shell script rust2wasm.sh. This will compile the Rust code and bundle the resulting WebAssembly into JavaScript files.

At this point, the .js files in dist can be copied by themselves into a project, or bundled into a package.

Benchmark

This module is in general 3x faster than the all-JavaScript sha1 package on npm, and the gap increases with the length of the source bytes. To run the benchmark, node benchmark.js from the benchmark folder.

$ node benchmark.js
Test string is 107 chars long
pure js x 136,241 ops/sec ±0.82% (89 runs sampled)
node crypto x 407,798 ops/sec ±6.95% (71 runs sampled)
rust/wasm x 497,166 ops/sec ±0.70% (90 runs sampled)

sha1-wasm's People

Contributors

toomanybees avatar

Watchers

 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.