Browse Source

20x performance for merkle tree lib (#1787)

* 20x performance for merkle tree lib

* add semicolon

* Update test/helpers/merkleTree.js

Co-Authored-By: Nicolás Venturo <nicolas.venturo@gmail.com>
Roman Storm 6 years ago
parent
commit
f358a03043
1 changed files with 3 additions and 3 deletions
  1. 3 3
      test/helpers/merkleTree.js

+ 3 - 3
test/helpers/merkleTree.js

@@ -5,10 +5,10 @@ class MerkleTree {
     // Filter empty strings and hash elements
     this.elements = elements.filter(el => el).map(el => keccak256(el));
 
-    // Deduplicate elements
-    this.elements = this.bufDedup(this.elements);
     // Sort elements
     this.elements.sort(Buffer.compare);
+    // Deduplicate elements
+    this.elements = this.bufDedup(this.elements);
 
     // Create layers
     this.layers = this.getLayers(this.elements);
@@ -113,7 +113,7 @@ class MerkleTree {
 
   bufDedup (elements) {
     return elements.filter((el, idx) => {
-      return this.bufIndexOf(el, elements) === idx;
+      return idx === 0 || !elements[idx - 1].equals(el);
     });
   }