@anglinb/city-hash

中文文档
1.1.0发布于 2025/07/09MITTypeScriptESM + CJS零依赖近 30 天下载 1.5万

TypeScript implementation of CityHash64 - fast, non-cryptographic hash function

原始使用文档
文档目录15 节
  1. Installation
  2. Usage
  3. API
  4. cityHash64(input: string | Uint8Array): bigint
  5. cityHash64NumMax(input: string | Uint8Array): number
  6. Features
  7. About CityHash
  8. ClickHouse Integration
  9. Direct SQL Query
  10. Create a ClickHouse Function
  11. Use in Table Operations
  12. Consistency Verification
  13. Performance
  14. License
  15. Credits

@anglinb/city-hash

A TypeScript implementation of Google's CityHash64 algorithm - a fast, non-cryptographic hash function.

Installation

npm install @anglinb/city-hash

Usage

import { cityHash64, cityHash64NumMax } from '@anglinb/city-hash';

// Hash a string
const hash1 = cityHash64("hello world");
console.log(hash1); // 12386028635079221413n

// Hash binary data
const data = new Uint8Array([1, 2, 3, 4, 5]);
const hash2 = cityHash64(data);
console.log(hash2); // 8996017160742164057n

// The result is always a BigInt
const hash3 = cityHash64("The quick brown fox jumps over the lazy dog");
console.log(hash3.toString()); // "16697807905646383735"

// Get a JavaScript-safe number (truncated to 53 bits)
const safeHash = cityHash64NumMax("hello world");
console.log(safeHash); // 1129659810357413 (safe JavaScript number)

API

cityHash64(input: string | Uint8Array): bigint

Computes a 64-bit hash of the input data using Google's CityHash algorithm.

Parameters:

  • input: The data to hash. Can be either a string or a Uint8Array.

Returns:

  • A 64-bit hash value as a BigInt.

cityHash64NumMax(input: string | Uint8Array): number

Computes a CityHash64 hash truncated to JavaScript's Number.MAX_SAFE_INTEGER (53 bits).

Parameters:

  • input: The data to hash. Can be either a string or a Uint8Array.

Returns:

  • A hash value as a JavaScript number, guaranteed to be ≤ Number.MAX_SAFE_INTEGER.

This function is useful when you need to work with hash values in JavaScript contexts that require regular numbers instead of BigInts, such as JSON serialization or certain database operations.

Features

  • Fast: Optimized for speed and designed for hash tables
  • High Quality: Produces well-distributed hash values with low collision rates
  • TypeScript: Full TypeScript support with proper type definitions
  • Compatible: Produces identical output to the original C implementation
  • Flexible: Accepts both strings and binary data (Uint8Array)

About CityHash

CityHash is a family of hash functions created by Google for use in hash tables and other applications where speed is more important than cryptographic security. CityHash64 specifically produces 64-bit hash values.

This implementation:

  • Matches the behavior of the original C implementation exactly
  • Handles both string and binary input data
  • Uses BigInt for proper 64-bit arithmetic in JavaScript
  • Is thoroughly tested with comprehensive test coverage

ClickHouse Integration

The cityHash64NumMax function is designed to be compatible with ClickHouse database operations. Here's how to reproduce the same truncated hash values in ClickHouse:

Direct SQL Query

-- Get the same result as cityHash64NumMax("hello world")
SELECT bitAnd(cityHash64('hello world'), 0x1FFFFFFFFFFFFF);
-- Result: 1129659810357413

Create a ClickHouse Function

You can create a user-defined function in ClickHouse to match cityHash64NumMax:

-- Create a function that matches cityHash64NumMax
CREATE FUNCTION cityHash64NumMax AS (input) -> bitAnd(cityHash64(input), 0x1FFFFFFFFFFFFF);

-- Usage examples
SELECT cityHash64NumMax('hello world');           -- 1129659810357413
SELECT cityHash64NumMax('test string');           -- 7852960286803079
SELECT cityHash64NumMax('another example');       -- 6487172654247519

Use in Table Operations

-- Create a table with a hash column
CREATE TABLE user_events (
    user_id String,
    event_data String,
    hash_value UInt64 MATERIALIZED cityHash64NumMax(event_data)
) ENGINE = MergeTree()
ORDER BY user_id;

-- Insert data (hash_value will be automatically calculated)
INSERT INTO user_events (user_id, event_data) VALUES 
    ('user1', 'login'),
    ('user2', 'purchase'),
    ('user1', 'logout');

-- Query using the hash
SELECT * FROM user_events WHERE hash_value = cityHash64NumMax('login');

Consistency Verification

You can verify that your JavaScript and ClickHouse implementations produce identical results:

// JavaScript
import { cityHash64NumMax } from '@anglinb/city-hash';
const jsResult = cityHash64NumMax('test input');
console.log(jsResult); // e.g., 1234567890123
-- ClickHouse
SELECT bitAnd(cityHash64('test input'), 0x1FFFFFFFFFFFFF);
-- Should return the same value: 1234567890123

Performance

CityHash is designed to be fast on modern processors. This TypeScript implementation maintains good performance while providing the safety and convenience of TypeScript.

License

MIT

Credits

Based on the original CityHash algorithm by Geoff Pike and Jyrki Alakuijala at Google.

版本与发布记录

@anglinb/city-hash 共发布 3 个版本,首次发布于 2025/07/02;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.0latest2025/07/091.3 年前
1.0.12025/07/021.3 年前
1.0.02025/07/021.3 年前

关于 @anglinb/city-hash 的常见问题

@anglinb/city-hash 是什么?

TypeScript implementation of CityHash64 - fast, non-cryptographic hash function

如何安装 @anglinb/city-hash?

在项目目录运行 npm install @anglinb/city-hash,或使用 pnpm add / yarn add / bun add。

@anglinb/city-hash 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@anglinb/city-hash 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@anglinb/city-hash 可以商用吗?

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

@anglinb/city-hash 的下载量有多少?

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

@anglinb/city-hash 还在维护吗?

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