imurmurhash

中文文档
0.1.4发布于 2013/08/25MITCommonJS零依赖近 30 天下载 6.3亿

An incremental implementation of MurmurHash3

原始使用文档
文档目录8 节
  1. Installation
  2. Quick Example
  3. Functions
  4. MurmurHash3 ([string], [seed])
  5. MurmurHash3.prototype.hash (string)
  6. MurmurHash3.prototype.result ()
  7. MurmurHash3.prototype.reset (seed)
  8. License (MIT)

iMurmurHash.JS

An incremental implementation of the MurmurHash3 (32-bit) hashing algorithm for JavaScript based on Gary Court's implementation.

Installation

To use iMurmurHash in the browser, download the latest version and include it as a script on your site.

<script type="text/javascript" src="/scripts/imurmurhash.min.js"></script>
<script>
// Your code here, access iMurmurHash using the global object MurmurHash3
</script>

To use iMurmurHash in Node.js, install the module using NPM:

npm install imurmurhash

Then simply include it in your scripts:

MurmurHash3 = require('imurmurhash');

Quick Example

// Create the initial hash
var hashState = MurmurHash3('string');

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

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

// Get a result
hashState.result();
// returns 0x29d3f1e3

Functions

MurmurHash3 ([string], [seed])

Get a hash state object, optionally initialized with the given string and seed. Seed must be a positive integer if provided. Calling this function without the new keyword will return a cached state object that has been reset. This is safe to use as long as the object is only used from a single thread and no other hashes are created while operating on this one. If this constraint cannot be met, you can use new to create a new state object. For example:

// Use the cached object, calling the function again will return the same
// object (but reset, so the current state would be lost)
hashState = MurmurHash3();
...

// Create a new object that can be safely used however you wish. Calling the
// function again will simply return a new state object, and no state loss
// will occur, at the cost of creating more objects.
hashState = new MurmurHash3();

Both methods can be mixed however you like if you have different use cases.


MurmurHash3.prototype.hash (string)

Incrementally add a string to the hash. This can be called as many times as you want for the hash state object, including after a calls to result(). Returns this so calls can be chained.


MurmurHash3.prototype.result ()

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
MurmurHash3('this is a test string').result();
// 0x70529328

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

MurmurHash3.prototype.reset (seed)

Reset the state object for reuse, optionally using the given seed (defaults to 0 like constructor). Returns this so calls can be chained.


License (MIT)

Copyright (c) 2013 Gary Court, Jens Taylor

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

版本与发布记录

imurmurhash 共发布 5 个版本,首次发布于 2013/08/01;过去 12 个月发布了 0 个版本。

版本发布日期距今
0.1.4latest2013/08/2513 年前
0.1.32013/08/0213 年前
0.1.22013/08/0213 年前
0.1.12013/08/0213 年前
0.0.12013/08/0113 年前

关于 imurmurhash 的常见问题

imurmurhash 是什么?

An incremental implementation of MurmurHash3

如何安装 imurmurhash?

在项目目录运行 npm install imurmurhash,或使用 pnpm add / yarn add / bun add。

imurmurhash 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/imurmurhash 是否存在。

imurmurhash 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

imurmurhash 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=0.8.19。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

imurmurhash 可以商用吗?

imurmurhash 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

imurmurhash 的下载量有多少?

根据 npm 官方下载统计,imurmurhash 在 2026/10/06 – 2026/11/04 这 30 天内被下载 626,546,482 次,日均约 20,884,883 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

imurmurhash 还在维护吗?

最新版本 0.1.4 发布于 2013/08/25(13 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2013/08/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。