Skip to content

Commit 3a97164

Browse files
🚴 perf(DeepIterator): Replace instanceof checks by proto.
1 parent 19846b3 commit 3a97164

File tree

4 files changed

+30
-20
lines changed

4 files changed

+30
-20
lines changed

src/0-core/_fast/fast-iterators/BackwardIterator.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,7 @@ BackwardIterator.prototype._upwardStep = function () {
2929
};
3030

3131
BackwardIterator.prototype._traverse = function (level, x) {
32-
if (x instanceof Node3) {
33-
this._level.push(level, level);
34-
this._stack.push(x.a, x.b);
35-
return x.c;
36-
}
37-
38-
assert(x instanceof Node2);
39-
this._level.push(level);
40-
this._stack.push(x.a);
41-
return x.b;
32+
assert(Number.isInteger(level) && level >= 0);
33+
assert(x instanceof Node2 || x instanceof Node3);
34+
return x._backward(level, this);
4235
};

src/0-core/_fast/fast-iterators/ForwardIterator.js

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,7 @@ ForwardIterator.prototype._upwardStep = function () {
2929
};
3030

3131
ForwardIterator.prototype._traverse = function (level, x) {
32-
if (x instanceof Node3) {
33-
this._level.push(level, level);
34-
this._stack.push(x.c, x.b);
35-
return x.a;
36-
}
37-
38-
assert(x instanceof Node2);
39-
this._level.push(level);
40-
this._stack.push(x.b);
41-
return x.a;
32+
assert(Number.isInteger(level) && level >= 0);
33+
assert(x instanceof Node2 || x instanceof Node3);
34+
return x._forward(level, this);
4235
};

src/2-node/2-Node2.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,15 @@ Node2.prototype.measure = function () {
1313
Node2.prototype._digit = function () {
1414
return new Two(this.a, this.b);
1515
};
16+
17+
Node2.prototype._forward = function (level, iterator) {
18+
iterator._level.push(level);
19+
iterator._stack.push(this.b);
20+
return this.a;
21+
};
22+
23+
Node2.prototype._backward = function (level, iterator) {
24+
iterator._level.push(level);
25+
iterator._stack.push(this.a);
26+
return this.b;
27+
};

src/2-node/3-Node3.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,15 @@ Node3.prototype.measure = function () {
1414
Node3.prototype._digit = function () {
1515
return new Three(this.a, this.b, this.c);
1616
};
17+
18+
Node3.prototype._forward = function (level, iterator) {
19+
iterator._level.push(level, level);
20+
iterator._stack.push(this.c, this.b);
21+
return this.a;
22+
};
23+
24+
Node3.prototype._backward = function (level, iterator) {
25+
iterator._level.push(level, level);
26+
iterator._stack.push(this.a, this.b);
27+
return this.c;
28+
};

0 commit comments

Comments
 (0)