git ssb

0+

wanderer🌟 / js-dfinity-radix-tree



Tree: e2cd325fdcc6c86a830407f0c4c8638f3a5ad25a

Files: e2cd325fdcc6c86a830407f0c4c8638f3a5ad25a / benchmark / radixTree-lmza.js

1592 bytesRaw
1const IPFS = require('ipfs')
2const crypto = require('crypto')
3const RadixTree = require('../')
4const cbor = require('borc')
5const lzma = require('lzma')
6
7// start ipfs
8const node = new IPFS({
9 start: false,
10 repo: './ipfs-repo'
11})
12
13node.on('ready', async () => {
14 const tree = new RadixTree({
15 dag: node.dag,
16 // root: { '/': 'zdpuAmHPs6YDFyFoaUHg5YgyNHoUGKzug5n5ZzdjUrk36Gnzg'}
17 })
18
19 const entries = 10000 //5117051
20 console.log('entries', entries)
21 for (let i = 0; i < entries; i++) {
22 const key = crypto.createHash('sha256').update(i.toString()).digest().slice(0, 20)
23 await tree.set(key, i)
24 }
25 console.log('flushing')
26 const sr = await tree.flush()
27 console.log('done', sr)
28
29 let proofSize = 0
30 let compressedSize = 0
31 const root = tree.root
32
33 for (let i = 0; i < entries; i++) {
34 const tree = new RadixTree({
35 dag: node.dag,
36 root: {'/': root['/']}
37 })
38 const key = crypto.createHash('sha256').update(i.toString()).digest().slice(0, 20)
39 await tree.get(key)
40 removeLink(tree.root['/'])
41 const encoded = cbor.encode(tree.root['/'])
42 proofSize += encoded.length
43 compressedSize += lzma.compress(encoded, 9).length
44 }
45 console.log('cbor size', proofSize / entries)
46 console.log('compressed size', compressedSize / entries)
47})
48
49function removeLink (root) {
50 if (root) {
51 if (root[0]) {
52 root[0] = root[0]['/']
53 }
54
55 if (root[1]) {
56 root[1] = root[1]['/']
57 }
58
59 if (!Buffer.isBuffer(root[1])) {
60 removeLink(root[1])
61 }
62
63 if (!Buffer.isBuffer(root[0])) {
64 removeLink(root[0])
65 }
66 }
67}
68

Built with git-ssb-web