2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

MurmurHash v3 の Node.js 実装の処理速度を比較 → murmurhash-js が速い

Posted at

短い ASCII 文字列の手軽なハッシュ値を欲しかったので、murmurhash v3 の pure JavaScript 実装の npm モジュールを4種ピックアップして、ベンチマーク比較してみました。
結論としては、手元では murmurhash-js が速かった。実装もめちゃくちゃシンプル

package Dependents Weekly Downloads ops/sec
murmurhash 151 231,041 1,796,786
imurmurhash 502 19,599,975 21,301,152
murmurhash3js 64 73,494 50,456,677
murmurhash-js 128 466,189 67,474,068

グラフが長いほうが速い。こんなに違いが出るとは。

murmur3.png

環境や入力する文字数によっては結果が違ったり、サポートする文字種や入力データ形式も異なると思いますので、使う用途に応じて再ベンチマークした方が良いかと。

# !/usr/bin/node

console.log(process.title, process.version, require("os").cpus().pop().model);

const Benchmark = require("benchmark");
const murmurhash = require("murmurhash");
const imurmurhash = require("imurmurhash");
const murmurhash3js = require("murmurhash3js");
const murmurhash_js = require("murmurhash-js");

const text = "string";

const suite = new Benchmark.Suite();

suite.add("murmurhash", () => murmurhash.v3(text));
suite.add("imurmurhash", () => imurmurhash(text).result());
suite.add("murmurhash3js", () => murmurhash3js.x86.hash32(text));
suite.add("murmurhash-js", () => murmurhash_js.murmur3(text));

suite.on("cycle", event => console.log(String(event.target)));
suite.on("complete", () => console.log('Fastest is ' + suite.filter('fastest').map('name')));

suite.run({async: true});

手元の環境 node v16.6.1 Apple M1 での実行結果

node v16.6.1 Apple M1
murmurhash x 1,796,786 ops/sec ±1.69% (80 runs sampled)
imurmurhash x 21,301,152 ops/sec ±0.37% (99 runs sampled)
murmurhash3js x 50,456,677 ops/sec ±0.33% (96 runs sampled)
murmurhash-js x 67,474,068 ops/sec ±0.42% (96 runs sampled)
Fastest is murmurhash-js
2
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?