xxhash というハッシュアルゴリズムがあり、パフォーマンス的には最速とのことである。
js でも幾つかライブラリが用意されているため、容易に使うことができる。

ということで簡単なコード書いて測ってみた。winston には profile 機能もあるとのことなので
それもついでに試してみる。。

比較するライブラリは xxhash と、 xxhashjs 最後に、crypto で SHA1 を作成してみて
100,000回ハッシュしてみた時間を測定していみる。
xxhash は、 C で記述されているので、スピーディなはず。
xxhashjs については、pureJS なライブラリで、ブラウザ上でも使うことができる。

 

var xxhashjs = require('xxhashjs');
var XXHash = require('xxhash');
var crypto = require('crypto');
var n = 100000;
var winston = require('winston');

winston.profile('xxhash');
for (var i = 0; i < n; i++) {
    XXHash.hash(new Buffer('abcd'), 0x0108);
}
winston.profile('xxhash');

winston.profile('xxhashjs');
for (var i = 0; i < n; i++) {
    xxhashjs('abcd', 0x0108);
}
winston.profile('xxhashjs');

winston.profile('sha1');
for (var i = 0; i < n; i++) {
    crypto.createHash('sha1').update('abcd').digest('hex');
}
winston.profile('sha1');
info: xxhash duration=162ms
info: xxhashjs duration=144ms
info: sha1 duration=439ms

文字数が少ないと、xxhashjs のほうが有利か。

では、文字数が 1,000文字のものをハッシュするとなるとどうなるか。

var xxhashjs = require('xxhashjs');
var XXHash = require('xxhash');
var crypto = require('crypto');
var n = 100000;
var length = 1000;
var winston = require('winston');
var randomstring = require('randomstring');

winston.profile('xxhash');
for (var i = 0; i < n; i++) {
    XXHash.hash(new Buffer(randomstring.generate(length)), 0x0108);
}
winston.profile('xxhash');

winston.profile('xxhashjs');
for (var i = 0; i < n; i++) {
    xxhashjs(randomstring.generate(length), 0x0108);
}
winston.profile('xxhashjs');

winston.profile('sha1');
for (var i = 0; i < n; i++) {
    crypto.createHash('sha1').update(randomstring.generate(length)).digest('hex');
}
winston.profile('sha1');
info: xxhash duration=5107ms
info: xxhashjs duration=5324ms
info: sha1 duration=5216ms

若干、xxhashjs のほうが不利になるようだ。

2 thoughts on “使った node.js ライブラリ – xxhashjs / xxhash

  1. That’s unexpected.
    xxHash should be something like >10x faster than SHA1 on long strings. At least for the C version.
    Maybe there is some overhead in node/js versions which cost more than the hash algorithm itself.

Leave a reply

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください