react-native-xxhash 0.1.7 → 0.1.9

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
@@ -2,7 +2,6 @@
2
2
 
3
3
  A React Native library for hashing strings using the fast and deterministic xxHash algorithm, written in C++ with JSI for high performance. This library provides support for both 64-bit and 128-bit hashing.
4
4
 
5
-
6
5
  ## Features
7
6
 
8
7
  - **High Performance**: xxHash is one of the fastest non-cryptographic hash functions.
@@ -10,6 +9,9 @@ A React Native library for hashing strings using the fast and deterministic xxHa
10
9
  - **128-bit and 64-bit Support**: Choose between 128-bit and 64-bit hash outputs based on your use case.
11
10
  - **Cross-Platform**: Supports both iOS and Android in React Native projects.
12
11
 
12
+ # Benchmarks (https://xxhash.com)
13
+ <img width="772" height="712" alt="Screenshot 2025-10-23 at 09 48 25" src="https://github.com/user-attachments/assets/792c220b-54cc-45d7-8e62-e26757d11e85" />
14
+
13
15
  ## Installation
14
16
 
15
17
  To install the library, use either `npm` or `yarn`:
@@ -92,7 +92,8 @@ android {
92
92
  cppFlags "-O2 -frtti -fexceptions -Wall -fstack-protector-all"
93
93
  abiFilters (*reactNativeArchitectures())
94
94
  arguments "-DANDROID_STL=c++_shared",
95
- "-DNODE_MODULES_DIR=${nodeModules}"
95
+ "-DNODE_MODULES_DIR=${nodeModules}",
96
+ "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON"
96
97
  }
97
98
  }
98
99
  }
@@ -1,40 +1,45 @@
1
1
  #include "react-native-xxhash.h"
2
2
 
3
- void xxhash::install(jsi::Runtime* rt_ptr) {
4
- jsi::Runtime& runtime = *rt_ptr;
3
+ constexpr size_t XXH128_HEX_LEN = 32;
4
+ constexpr size_t XXH64_HEX_LEN = 16;
5
+ constexpr size_t XXH128_HEX_LEN_NT = XXH128_HEX_LEN + 1;
6
+ constexpr size_t XXH64_HEX_LEN_NT = XXH64_HEX_LEN + 1;
7
+
8
+ void xxhash::install(jsi::Runtime *rt_ptr) {
9
+ jsi::Runtime &runtime = *rt_ptr;
5
10
 
6
11
  jsi::Function hash128 = jsi::Function::createFromHostFunction(
7
12
  runtime, jsi::PropNameID::forAscii(runtime, "hash128"), 1,
8
- [](jsi::Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args,
13
+ [](jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *args,
9
14
  size_t count) {
10
- const jsi::Value& arg = args[0];
15
+ const jsi::Value &arg = args[0];
11
16
 
12
17
  if (!arg.isString()) [[unlikely]] {
13
18
  throw jsi::JSError(rt, "Argument is not a 'string'");
14
19
  }
15
20
 
16
- char result[33];
21
+ char result[XXH128_HEX_LEN_NT];
17
22
 
18
23
  xxhash::make_hash_128(arg.asString(rt).utf8(rt), result);
19
24
 
20
- return jsi::String::createFromUtf8(rt, result);
25
+ return jsi::String::createFromAscii(rt, result, XXH128_HEX_LEN);
21
26
  });
22
27
 
23
28
  jsi::Function hash64 = jsi::Function::createFromHostFunction(
24
29
  runtime, jsi::PropNameID::forAscii(runtime, "hash64"), 1,
25
- [](jsi::Runtime& rt, const jsi::Value& thisVal, const jsi::Value* args,
30
+ [](jsi::Runtime &rt, const jsi::Value &thisVal, const jsi::Value *args,
26
31
  size_t count) {
27
- const jsi::Value& arg = args[0];
32
+ const jsi::Value &arg = args[0];
28
33
 
29
34
  if (!arg.isString()) [[unlikely]] {
30
35
  throw jsi::JSError(rt, "Argument is not a 'string'");
31
36
  }
32
37
 
33
- char result[17];
38
+ char result[XXH64_HEX_LEN_NT];
34
39
 
35
40
  xxhash::make_hash_64(arg.asString(rt).utf8(rt), result);
36
41
 
37
- return jsi::String::createFromUtf8(rt, result);
42
+ return jsi::String::createFromAscii(rt, result, XXH64_HEX_LEN);
38
43
  });
39
44
 
40
45
  runtime.global().setProperty(runtime, "__xxhash128", std::move(hash128));
@@ -1,10 +1,10 @@
1
1
  #pragma once
2
2
  #ifndef XXHASH_H
3
3
  #define XXHASH_H
4
- #include <jsi/jsi.h>
4
+ #include "xxhash.h"
5
5
  #include <iomanip>
6
+ #include <jsi/jsi.h>
6
7
  #include <sstream>
7
- #include "xxhash.h"
8
8
 
9
9
  using namespace facebook;
10
10
 
@@ -12,16 +12,38 @@ namespace xxhash {
12
12
 
13
13
  inline void make_hash_64(const std::string_view str, char result[17]) noexcept {
14
14
  XXH64_hash_t hash = XXH3_64bits(str.data(), str.size());
15
- std::snprintf(result, 17, "%016llx", hash);
16
- };
17
15
 
18
- inline void make_hash_128(const std::string_view str,
19
- char result[33]) noexcept {
20
- XXH128_hash_t hash = XXH3_128bits(str.data(), str.size());
21
- std::snprintf(result, 33, "%016llx%016llx", hash.high64, hash.low64);
16
+ static constexpr char hex[] = "0123456789abcdef";
17
+
18
+ for (int i = 0; i < 16; ++i) {
19
+ const uint8_t nibble = (hash >> ((15 - i) * 4)) & 0xF;
20
+ result[i] = hex[nibble];
21
+ }
22
+
23
+ result[16] = '\0';
22
24
  };
23
25
 
24
- void install(jsi::Runtime* rt);
25
- } // namespace xxhash
26
+ inline void make_hash_128(std::string_view str, char result[33]) noexcept {
27
+ const XXH128_hash_t hash = XXH3_128bits(str.data(), str.size());
28
+
29
+ static constexpr char hex[] = "0123456789abcdef";
30
+
31
+ // high64 → first 16
32
+ for (int i = 0; i < 16; ++i) {
33
+ const uint8_t nibble = (hash.high64 >> ((15 - i) * 4)) & 0xF;
34
+ result[i] = hex[nibble];
35
+ }
36
+
37
+ // low64 → next 16
38
+ for (int i = 0; i < 16; ++i) {
39
+ const uint8_t nibble = (hash.low64 >> ((15 - i) * 4)) & 0xF;
40
+ result[16 + i] = hex[nibble];
41
+ }
42
+
43
+ result[32] = '\0';
44
+ }
45
+
46
+ void install(jsi::Runtime *rt);
47
+ } // namespace xxhash
26
48
 
27
49
  #endif /* XXHASH_H */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-xxhash",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "description": "💪 A high-performance React Native library for generating xxHash hashes using C++ and JSI",
5
5
  "main": "./lib/commonjs/index.js",
6
6
  "module": "./lib/module/index.js",