React Native 的快速、記憶體高效的 Trie 實作。使用 Tessel 的 HAT-trie。
npm install react-native-fast-trie
yarn add react-native-fast-trie
基準測試是與在發布模式下建構的真實設備上常用的 JS 實作(trie 類型)進行比較。您可以在您的裝置上建立範例專案來重現這些結果。
測試如下:
裝置 | 單字表 | 批量插入 | 所有單字表 | 包含 | 尋找 |
---|---|---|---|---|---|
像素5 | 速度提高 4.64 倍 | 速度提高 16.86 倍 | 速度提高 7.76 倍 | 速度提高 2.94 倍 | 速度提高 24.63 倍 |
像素3a | 速度提高 3.26 倍 | 速度提高 14.67 倍 | 速度提高 5.54 倍 | 速度提高 3.06 倍 | 速度提高 26.23 倍 |
銀河A10e | 速度提高 2.94 倍 | 速度提高 9.83 倍 | 速度提高 4.84 倍 | 速度提高 3.95 倍 | 速度提高 11.47 倍 |
iPhone 15 Pro 最大 | 速度提高 3.78 倍 | 速度提高 10.33 倍 | 速度提高 5.13 倍 | 速度提高 3.43 倍 | 速度提高 23.83 倍 |
iPhone 11 Pro 最大 | 速度提高 4.65 倍 | 速度提高 13.12 倍 | 速度提高 5.21 倍 | 速度提高 3.35 倍 | 速度提高 23.79 倍 |
iPhone 7 | 速度提高 3.58 倍 | 速度提高 12.03 倍 | 速度提高 5.65 倍 | 速度提高 3.46 倍 | 速度提高 26.86 倍 |
這些基準測試的螢幕截圖可以在基準測試資料夾中找到。
// index.js
import { FastTrie } from 'react-native-fast-trie' ;
const trie = new FastTrie ( ) ;
console . log ( trie . contains ( 'test' ) ) ; // false
trie . insert ( 'test' ) ;
console . log ( trie . contains ( 'test' ) ) ; // true
console . log ( trie . find ( 'te' ) ) ; // ['test']
trie . batchInsert ( [ 'test2' , 'test3' ] ) ;
// Limit to only 2 results
console . log ( trie . find ( 'te' , 2 ) ) ; // ['test2', 'test3']
trie . delete ( 'test2' ) ;
console . log ( trie . contains ( 'test2' ) ) ; // false
FastTrie 是專為 React Native 應用程式設計的高效能 trie 實作。它提供了插入元素、檢查元素是否存在以及查找具有特定前綴的元素的高效操作。此實作提供了自訂選項來平衡速度和記憶體使用量。
FastTrieOptions
類型此類型允許配置 FastTrie 實例。
burstThreshold?: number
指定突發發生前數組哈希節點的最大大小。建議使用較高的值(例如 16,384)進行精確搜索,而預設值 1024 則針對前綴搜索進行最佳化。
maxLoadFactor?: number
確定 trie 的負載因子。較低的值會提高速度,而較高的值會減少記憶體使用量。預設值為 8.0。
FastTrie
類建立 FastTrie 的新實例。
參數:
options: FastTrieOptions
(可選)burstThreshold
和maxLoadFactor
。例子:
const trie = new FastTrie ( { burstThreshold : 2048 , maxLoadFactor : 10.0 } ) ;
insert(item: string): void
將字串插入到 trie 中。
trie . insert ( 'example' ) ;
batchInsert(items: string[]): void
透過一次操作將多個字串插入到 trie 中。此方法針對批量插入進行了最佳化,並且比單獨插入項目更有效。
trie . batchInsert ( [ 'apple' , 'apricot' , 'banana' ] ) ;
contains(item: string): boolean
檢查 trie 中是否存在字串。
const isPresent = trie . contains ( 'example' ) ;
find(prefix: string, maxResults?: number): boolean
尋找 trie 中以給定前綴開頭的所有字串。
const results = trie . find ( 'ex' , 10 ) ;
delete(item: string): void
如果字串存在於 trie 中,則刪除該字串
trie . delete ( 'apple' ) ;
請參閱貢獻指南,了解如何為儲存庫和開發工作流程做出貢獻。
麻省理工學院
使用 create-react-native-library 製作