@keyv/bigmap 1.1.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # @keyv/redis [<img width="100" align="right" src="https://jaredwray.com/images/keyv-symbol.svg" alt="keyv">](https://github.com/jaredwra/keyv)
1
+ # @keyv/bigmap [<img width="100" align="right" src="https://jaredwray.com/images/keyv-symbol.svg" alt="keyv">](https://github.com/jaredwra/keyv)
2
2
 
3
3
  > Bigmap for Keyv
4
4
 
@@ -18,7 +18,6 @@
18
18
  * Maintained regularly with a focus on performance and reliability.
19
19
 
20
20
  # Table of Contents
21
-
22
21
  - [Features](#features)
23
22
  - [Installation](#installation)
24
23
  - [Overview](#overview)
@@ -26,38 +25,37 @@
26
25
  - [Custom Store Size](#custom-store-size)
27
26
  - [Custom Hash Function](#custom-hash-function)
28
27
  - [Iteration](#iteration)
29
- - [For...of Loop](#forof-loop)
30
- - [forEach](#foreach)
31
- - [Keys, Values, and Entries](#keys-values-and-entries)
28
+ - [For...of Loop](#forof-loop)
29
+ - [forEach](#foreach)
30
+ - [Keys, Values, and Entries](#keys-values-and-entries)
32
31
  - [Advanced Features](#advanced-features)
33
- - [Type Safety with Generics](#type-safety-with-generics)
34
- - [Large-Scale Data](#large-scale-data)
32
+ - [Type Safety with Generics](#type-safety-with-generics)
33
+ - [Large-Scale Data](#large-scale-data)
35
34
  - [Using with Keyv](#using-with-keyv)
36
- - [`createKeyv(options?)`](#createkeyvoptions)
37
- - [With Custom Options](#with-custom-options)
38
- - [Type Safety](#type-safety)
39
- - [Integration with Keyv Ecosystem](#integration-with-keyv-ecosystem)
35
+ - [createKeyv](#createkeyv)
36
+ - [With Custom Options](#with-custom-options)
37
+ - [Type Safety](#type-safety)
38
+ - [Integration with Keyv Ecosystem](#integration-with-keyv-ecosystem)
40
39
  - [API](#api)
41
- - [Constructor](#constructor)
42
- - [Properties](#properties)
43
- - [Methods](#methods)
44
- - [`set(key, value)`](#setkey-value)
45
- - [`get(key)`](#getkey)
46
- - [`has(key)`](#haskey)
47
- - [`delete(key)`](#deletekey)
48
- - [`clear()`](#clear)
49
- - [`forEach(callbackfn, thisArg?)`](#foreachcallbackfn-thisarg)
50
- - [`keys()`](#keys)
51
- - [`values()`](#values)
52
- - [`entries()`](#entries)
53
- - [`[Symbol.iterator]()`](#symboliterator)
54
- - [`getStore(key)`](#getstorekey)
55
- - [`getStoreMap(index)`](#getstoremapindex)
56
- - [`initStore()`](#initstore)
40
+ - [Constructor](#constructor)
41
+ - [Properties](#properties)
42
+ - [Methods](#methods)
43
+ - [set](#set)
44
+ - [get](#get)
45
+ - [has](#has)
46
+ - [delete](#delete)
47
+ - [clear](#clear)
48
+ - [forEach](#foreach)
49
+ - [keys](#keys)
50
+ - [values](#values)
51
+ - [entries](#entries)
52
+ - [Symbol.iterator](#symboliterator)
53
+ - [getStore](#getstorekey)
54
+ - [getStoreMap](#getstoremapindex)
55
+ - [initStore](#initstore)
57
56
  - [Types](#types)
58
- - [`StoreHashFunction`](#storehashfunction)
59
- - [`defaultHashFunction(key, storeSize)`](#defaulthashfunctionkey-storesize)
60
- - [`djb2Hash(string, min?, max?)`](#djb2hashstring-min-max)
57
+ - [StoreHashFunction](#storehashfunction)
58
+ - [defaultHashFunction(key, storeSize)](#defaulthashfunctionkey-storesize)
61
59
  - [Contributing](#contributing)
62
60
  - [License](#license)
63
61
 
@@ -99,7 +97,7 @@ console.log(bigMap.size); // 1
99
97
  bigMap.clear();
100
98
  ```
101
99
 
102
- ## Custom Store Size
100
+ # Custom Store Size
103
101
 
104
102
  By default, BigMap uses 4 internal Map instances. You can configure this:
105
103
 
@@ -109,7 +107,7 @@ const bigMap = new BigMap<string, number>({ storeSize: 10 });
109
107
 
110
108
  **Note:** Changing the `storeSize` after initialization will clear all entries.
111
109
 
112
- ## Custom Hash Function
110
+ # Custom Hash Function
113
111
 
114
112
  Provide your own hash function for key distribution:
115
113
 
@@ -123,11 +121,43 @@ const bigMap = new BigMap<string, string>({
123
121
  });
124
122
  ```
125
123
 
126
- ## Iteration
124
+ ## Using Hashery for Hash Functions
125
+
126
+ [Hashery](https://github.com/jaredwray/hashery) is a powerful hashing library that provides multiple hash algorithms. You can use it for better key distribution and it is available as an export:
127
+
128
+ ```typescript
129
+ import { BigMap, Hashery } from '@keyv/bigmap';
130
+
131
+ const hashery = new Hashery();
132
+
133
+ // Using Hashery's toNumberSync for deterministic key distribution
134
+ const bigMap = new BigMap<string, string>({
135
+ storeHashFunction: (key: string, storeSize: number) => {
136
+ return hashery.toNumberSync(key, { min: 0, max: storeSize - 1 });
137
+ }
138
+ });
139
+
140
+ // You can also use different algorithms
141
+ const hasheryFnv1 = new Hashery({ defaultAlgorithmSync: 'fnv1' });
142
+
143
+ const bigMapWithFnv1 = new BigMap<string, string>({
144
+ storeHashFunction: (key: string, storeSize: number) => {
145
+ return hasheryFnv1.toNumberSync(key, { min: 0, max: storeSize - 1 });
146
+ }
147
+ });
148
+ ```
149
+
150
+ Hashery supports multiple synchronous hash algorithms:
151
+ - **djb2** - Fast hash function (default)
152
+ - **fnv1** - Excellent distribution for hash tables
153
+ - **murmer** - MurmurHash algorithm
154
+ - **crc32** - Cyclic Redundancy Check
155
+
156
+ # Iteration
127
157
 
128
158
  BigMap supports all standard Map iteration methods:
129
159
 
130
- ### For...of Loop
160
+ ## For...of Loop
131
161
 
132
162
  ```typescript
133
163
  const bigMap = new BigMap<string, number>();
@@ -172,9 +202,9 @@ for (const [key, value] of bigMap.entries()) {
172
202
  }
173
203
  ```
174
204
 
175
- ## Advanced Features
205
+ # Advanced Features
176
206
 
177
- ### Type Safety with Generics
207
+ ## Type Safety with Generics
178
208
 
179
209
  ```typescript
180
210
  interface User {
@@ -186,7 +216,7 @@ const userMap = new BigMap<string, User>();
186
216
  userMap.set('user1', { id: 1, name: 'Alice' });
187
217
  ```
188
218
 
189
- ### Large-Scale Data
219
+ ## Large-Scale Data
190
220
 
191
221
  BigMap is designed to handle millions of entries:
192
222
 
@@ -201,11 +231,11 @@ for (let i = 0; i < 20000000; i++) {
201
231
  console.log(bigMap.size); // 20000000
202
232
  ```
203
233
 
204
- ## Using with Keyv
234
+ # Using with Keyv
205
235
 
206
236
  BigMap can be used as a storage adapter for [Keyv](https://github.com/jaredwray/keyv), providing a scalable in-memory store with TTL support.
207
237
 
208
- ### `createKeyv(options?)`
238
+ ## createKeyv
209
239
 
210
240
  The `createKeyv` function creates a Keyv instance with BigMap as the storage adapter.
211
241
 
@@ -241,7 +271,7 @@ await keyv.delete('user:123');
241
271
  await keyv.clear();
242
272
  ```
243
273
 
244
- ### With Custom Options
274
+ ## With Custom Options
245
275
 
246
276
  ```typescript
247
277
  import { createKeyv } from '@keyv/bigmap';
@@ -259,7 +289,7 @@ const keyv = createKeyv({
259
289
  });
260
290
  ```
261
291
 
262
- ### Type Safety
292
+ ## Type Safety
263
293
 
264
294
  ```typescript
265
295
  import { createKeyv } from '@keyv/bigmap';
@@ -281,7 +311,7 @@ await keyv.set('product:1', {
281
311
  const product = await keyv.get<Product>('product:1');
282
312
  ```
283
313
 
284
- ### Integration with Keyv Ecosystem
314
+ # Integration with Keyv Ecosystem
285
315
 
286
316
  BigMap works seamlessly with the Keyv ecosystem:
287
317
 
@@ -305,9 +335,9 @@ for await (const [key, value] of cache.iterator()) {
305
335
 
306
336
  # API
307
337
 
308
- # Constructor
338
+ ## Constructor
309
339
 
310
- `new BigMap<K, V>(options?)`
340
+ `new BigMap<K, V>(options?)`
311
341
 
312
342
  Creates a new BigMap instance.
313
343
 
@@ -353,9 +383,9 @@ bigMap.storeHashFunction = (key, storeSize) => key.length % storeSize;
353
383
  console.log(bigMap.store.length); // 8
354
384
  ```
355
385
 
356
- # Methods
386
+ ## Methods
357
387
 
358
- ## `set(key, value)`
388
+ ### set
359
389
 
360
390
  Sets the value for a key in the map.
361
391
 
@@ -370,7 +400,7 @@ Sets the value for a key in the map.
370
400
  bigMap.set('user123', { name: 'Alice' });
371
401
  ```
372
402
 
373
- ## `get(key)`
403
+ ### get
374
404
 
375
405
  Gets the value associated with a key.
376
406
 
@@ -384,7 +414,7 @@ Gets the value associated with a key.
384
414
  const value = bigMap.get('user123');
385
415
  ```
386
416
 
387
- ## `has(key)`
417
+ ### has
388
418
 
389
419
  Checks if a key exists in the map.
390
420
 
@@ -400,7 +430,7 @@ if (bigMap.has('user123')) {
400
430
  }
401
431
  ```
402
432
 
403
- ## `delete(key)`
433
+ ### delete
404
434
 
405
435
  Deletes a key-value pair from the map.
406
436
 
@@ -414,7 +444,7 @@ Deletes a key-value pair from the map.
414
444
  const deleted = bigMap.delete('user123');
415
445
  ```
416
446
 
417
- ## `clear()`
447
+ ### clear
418
448
 
419
449
  Removes all entries from the map.
420
450
 
@@ -426,7 +456,7 @@ bigMap.clear();
426
456
  console.log(bigMap.size); // 0
427
457
  ```
428
458
 
429
- ## `forEach(callbackfn, thisArg?)`
459
+ ### forEach
430
460
 
431
461
  Executes a provided function once for each key-value pair.
432
462
 
@@ -434,7 +464,7 @@ Executes a provided function once for each key-value pair.
434
464
  - `callbackfn` (function): Function to execute for each entry
435
465
  - `value` (V): The value of the current entry
436
466
  - `key` (K): The key of the current entry
437
- - `map` (Map<K, V>): The BigMap instance
467
+ - `map` (`Map<K, V>`): The BigMap instance
438
468
  - `thisArg` (optional): Value to use as `this` when executing the callback
439
469
 
440
470
  **Returns:** `void`
@@ -452,7 +482,7 @@ bigMap.forEach(function(value) {
452
482
  }, context);
453
483
  ```
454
484
 
455
- ## `keys()`
485
+ ### keys
456
486
 
457
487
  Returns an iterator of all keys in the map.
458
488
 
@@ -465,7 +495,7 @@ for (const key of bigMap.keys()) {
465
495
  }
466
496
  ```
467
497
 
468
- ## `values()`
498
+ ### values
469
499
 
470
500
  Returns an iterator of all values in the map.
471
501
 
@@ -478,7 +508,7 @@ for (const value of bigMap.values()) {
478
508
  }
479
509
  ```
480
510
 
481
- ## `entries()`
511
+ ### entries
482
512
 
483
513
  Returns an iterator of all key-value pairs in the map.
484
514
 
@@ -491,7 +521,7 @@ for (const [key, value] of bigMap.entries()) {
491
521
  }
492
522
  ```
493
523
 
494
- ## `[Symbol.iterator]()`
524
+ ### Symbol.iterator
495
525
 
496
526
  Returns an iterator for the map (same as `entries()`). Enables `for...of` loops.
497
527
 
@@ -504,7 +534,7 @@ for (const [key, value] of bigMap) {
504
534
  }
505
535
  ```
506
536
 
507
- ## `getStore(key)`
537
+ ### getStore
508
538
 
509
539
  Gets the internal Map instance for a specific key.
510
540
 
@@ -518,7 +548,7 @@ Gets the internal Map instance for a specific key.
518
548
  const store = bigMap.getStore('user123');
519
549
  ```
520
550
 
521
- ## `getStoreMap(index)`
551
+ ### getStoreMap
522
552
 
523
553
  Gets the internal Map instance at a specific index.
524
554
 
@@ -534,15 +564,15 @@ Gets the internal Map instance at a specific index.
534
564
  const firstMap = bigMap.getStoreMap(0);
535
565
  ```
536
566
 
537
- ## `initStore()`
567
+ ### initStore
538
568
 
539
569
  Initializes the internal store with empty Map instances. Called automatically during construction.
540
570
 
541
571
  **Returns:** `void`
542
572
 
543
- # Types
573
+ ## Types
544
574
 
545
- ## `StoreHashFunction`
575
+ ### StoreHashFunction
546
576
 
547
577
  Type definition for custom hash functions.
548
578
 
@@ -556,9 +586,9 @@ type StoreHashFunction = (key: string, storeSize: number) => number;
556
586
 
557
587
  **Returns:** `number` - The index of the store to use (0 to storeSize - 1)
558
588
 
559
- ## `defaultHashFunction(key, storeSize)`
589
+ ### defaultHashFunction
560
590
 
561
- The default hash function using DJB2 algorithm.
591
+ The default hash function using DJB2 algorithm from [Hashery](https://npmjs.com/package/hashery):
562
592
 
563
593
  **Example:**
564
594
  ```typescript
@@ -567,7 +597,7 @@ import { defaultHashFunction } from '@keyv/bigmap';
567
597
  const index = defaultHashFunction('myKey', 4);
568
598
  ```
569
599
 
570
- ## `djb2Hash(string, min?, max?)`
600
+ ### djb2Hash
571
601
 
572
602
  DJB2 hash algorithm implementation.
573
603
 
package/dist/index.cjs CHANGED
@@ -21,25 +21,19 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
23
  BigMap: () => BigMap,
24
+ Hashery: () => import_hashery2.Hashery,
24
25
  Keyv: () => import_keyv2.Keyv,
25
26
  createKeyv: () => createKeyv,
26
- defaultHashFunction: () => defaultHashFunction,
27
- djb2Hash: () => djb2Hash
27
+ defaultHashFunction: () => defaultHashFunction
28
28
  });
29
29
  module.exports = __toCommonJS(index_exports);
30
+ var import_hashery = require("hashery");
30
31
  var import_hookified = require("hookified");
31
32
  var import_keyv = require("keyv");
33
+ var import_hashery2 = require("hashery");
32
34
  var import_keyv2 = require("keyv");
33
35
  function defaultHashFunction(key, storeSize) {
34
- return djb2Hash(key, 0, storeSize - 1);
35
- }
36
- function djb2Hash(string_, min = 0, max = 10) {
37
- let hash = 5381;
38
- for (let i = 0; i < string_.length; i++) {
39
- hash = hash * 33 ^ string_.charCodeAt(i);
40
- }
41
- const range = max - min + 1;
42
- return min + Math.abs(hash) % range;
36
+ return new import_hashery.Hashery().toNumberSync(key, { min: 0, max: storeSize - 1 });
43
37
  }
44
38
  var BigMap = class extends import_hookified.Hookified {
45
39
  _storeSize = 4;
@@ -267,8 +261,8 @@ function createKeyv(options) {
267
261
  // Annotate the CommonJS export names for ESM import in node:
268
262
  0 && (module.exports = {
269
263
  BigMap,
264
+ Hashery,
270
265
  Keyv,
271
266
  createKeyv,
272
- defaultHashFunction,
273
- djb2Hash
267
+ defaultHashFunction
274
268
  });
package/dist/index.d.cts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { HookifiedOptions, Hookified } from 'hookified';
2
2
  import { Keyv } from 'keyv';
3
3
  export { Keyv } from 'keyv';
4
+ export { Hashery } from 'hashery';
4
5
 
5
6
  type MapInterfacee<K, V> = {
6
7
  readonly size: number;
@@ -17,7 +18,6 @@ type MapInterfacee<K, V> = {
17
18
  };
18
19
  type StoreHashFunction = (key: string, storeSize: number) => number;
19
20
  declare function defaultHashFunction(key: string, storeSize: number): number;
20
- declare function djb2Hash(string_: string, min?: number, max?: number): number;
21
21
  type BigMapOptions = {
22
22
  /**
23
23
  * Optional size of the store. The default is 4 maps objects.
@@ -154,4 +154,4 @@ declare class BigMap<K, V> extends Hookified implements MapInterfacee<K, V> {
154
154
  */
155
155
  declare function createKeyv<K = string, V = unknown>(options?: BigMapOptions): Keyv;
156
156
 
157
- export { BigMap, type BigMapOptions, type MapInterfacee, type StoreHashFunction, createKeyv, defaultHashFunction, djb2Hash };
157
+ export { BigMap, type BigMapOptions, type MapInterfacee, type StoreHashFunction, createKeyv, defaultHashFunction };
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import { HookifiedOptions, Hookified } from 'hookified';
2
2
  import { Keyv } from 'keyv';
3
3
  export { Keyv } from 'keyv';
4
+ export { Hashery } from 'hashery';
4
5
 
5
6
  type MapInterfacee<K, V> = {
6
7
  readonly size: number;
@@ -17,7 +18,6 @@ type MapInterfacee<K, V> = {
17
18
  };
18
19
  type StoreHashFunction = (key: string, storeSize: number) => number;
19
20
  declare function defaultHashFunction(key: string, storeSize: number): number;
20
- declare function djb2Hash(string_: string, min?: number, max?: number): number;
21
21
  type BigMapOptions = {
22
22
  /**
23
23
  * Optional size of the store. The default is 4 maps objects.
@@ -154,4 +154,4 @@ declare class BigMap<K, V> extends Hookified implements MapInterfacee<K, V> {
154
154
  */
155
155
  declare function createKeyv<K = string, V = unknown>(options?: BigMapOptions): Keyv;
156
156
 
157
- export { BigMap, type BigMapOptions, type MapInterfacee, type StoreHashFunction, createKeyv, defaultHashFunction, djb2Hash };
157
+ export { BigMap, type BigMapOptions, type MapInterfacee, type StoreHashFunction, createKeyv, defaultHashFunction };
package/dist/index.js CHANGED
@@ -1,17 +1,11 @@
1
1
  // src/index.ts
2
+ import { Hashery } from "hashery";
2
3
  import { Hookified } from "hookified";
3
4
  import { Keyv } from "keyv";
5
+ import { Hashery as Hashery2 } from "hashery";
4
6
  import { Keyv as Keyv2 } from "keyv";
5
7
  function defaultHashFunction(key, storeSize) {
6
- return djb2Hash(key, 0, storeSize - 1);
7
- }
8
- function djb2Hash(string_, min = 0, max = 10) {
9
- let hash = 5381;
10
- for (let i = 0; i < string_.length; i++) {
11
- hash = hash * 33 ^ string_.charCodeAt(i);
12
- }
13
- const range = max - min + 1;
14
- return min + Math.abs(hash) % range;
8
+ return new Hashery().toNumberSync(key, { min: 0, max: storeSize - 1 });
15
9
  }
16
10
  var BigMap = class extends Hookified {
17
11
  _storeSize = 4;
@@ -238,8 +232,8 @@ function createKeyv(options) {
238
232
  }
239
233
  export {
240
234
  BigMap,
235
+ Hashery2 as Hashery,
241
236
  Keyv2 as Keyv,
242
237
  createKeyv,
243
- defaultHashFunction,
244
- djb2Hash
238
+ defaultHashFunction
245
239
  };
package/package.json CHANGED
@@ -1,15 +1,21 @@
1
1
  {
2
2
  "name": "@keyv/bigmap",
3
- "version": "1.1.0",
3
+ "version": "1.3.0",
4
4
  "description": "Bigmap for Keyv",
5
5
  "type": "module",
6
- "main": "dist/index.cjs",
6
+ "main": "dist/index.js",
7
7
  "module": "dist/index.js",
8
8
  "types": "dist/index.d.ts",
9
9
  "exports": {
10
10
  ".": {
11
- "require": "./dist/index.cjs",
12
- "import": "./dist/index.js"
11
+ "require": {
12
+ "types": "./dist/index.d.cts",
13
+ "default": "./dist/index.cjs"
14
+ },
15
+ "import": {
16
+ "types": "./dist/index.d.ts",
17
+ "default": "./dist/index.js"
18
+ }
13
19
  }
14
20
  },
15
21
  "repository": {
@@ -32,18 +38,19 @@
32
38
  },
33
39
  "homepage": "https://github.com/jaredwray/keyv",
34
40
  "dependencies": {
35
- "hookified": "^1.12.2"
41
+ "hashery": "^1.2.0",
42
+ "hookified": "^1.13.0"
36
43
  },
37
44
  "devDependencies": {
38
- "@biomejs/biome": "^2.2.6",
45
+ "@biomejs/biome": "^2.3.6",
39
46
  "@faker-js/faker": "^10.1.0",
40
- "@vitest/coverage-v8": "^3.2.4",
41
- "rimraf": "^6.0.1",
47
+ "@vitest/coverage-v8": "^4.0.10",
48
+ "rimraf": "^6.1.0",
42
49
  "tsd": "^0.33.0",
43
- "vitest": "^3.2.4"
50
+ "vitest": "^4.0.10"
44
51
  },
45
52
  "peerDependencies": {
46
- "keyv": "^5.5.3"
53
+ "keyv": "^5.5.4"
47
54
  },
48
55
  "tsd": {
49
56
  "directory": "test"