Skip to content

Commit 542f69d

Browse files
🚴 perf(prepend): Prepend by concatenation instead of consing.
1 parent 118fb83 commit 542f69d

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

src/3-tree/base/Tree.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import {prepend} from '../../0-core/index.js';
2-
31
/**
42
* Base class for all implementations.
53
*/
@@ -95,6 +93,6 @@ Tree.prototype.dropUntil = function (predicate) {
9593
*
9694
* @returns {Tree} The output tree.
9795
*/
98-
Tree.prototype.prepend = function (iterable) {
99-
return prepend(this, iterable);
100-
};
96+
// Tree.prototype.prepend = function (iterable) {
97+
// return prepend(this, iterable);
98+
// };

src/3-tree/implementations/0-Empty.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,11 @@ Empty.prototype.cons = function (value) {
4848
return new Single(this.M, value);
4949
};
5050

51-
Empty.prototype.append = function (iterable) {
52-
return _from_by_filling(this.M, iterable);
53-
};
51+
Empty.prototype.prepend =
52+
// eslint-disable-next-line no-multi-assign
53+
Empty.prototype.append = function (iterable) {
54+
return _from_by_filling(this.M, iterable);
55+
};
5456

5557
Empty.prototype.concat = function (other) {
5658
assert(other instanceof Tree);

src/3-tree/implementations/1-Single.js

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import {cache, Split} from '../../0-core/index.js';
44
import {One} from '../../1-digit/index.js';
55
import _append_small_list from '../../0-core/_fast/_append_small_list.js';
66
import {_from_medium_list} from '../../0-core/_fast/_from_medium_list.js';
7-
import _fill_right from '../../0-core/_fast/_fill_right.js';
87
import isSameMeasure from '../../_debug/isSameMeasure.js';
8+
import _from_by_filling from '../../0-core/_fast/_from_by_filling.js';
99
import {Empty, Deep} from './index.js';
1010

1111
export function Single(M, value) {
@@ -60,17 +60,12 @@ Single.prototype._UNSAFE_push =
6060
);
6161
};
6262

63-
Single.prototype.append = function (iterable) {
64-
const it = iterable[Symbol.iterator]();
65-
66-
const event = it.next();
67-
if (event.done) return this;
68-
const x1 = event.value;
69-
70-
const left = new One(this.a);
71-
const middle = new Empty(cache(this.M));
63+
Single.prototype.prepend = function (iterable) {
64+
return _from_by_filling(this.M, iterable).push(this.a);
65+
};
7266

73-
return _fill_right(this.M, left, middle, x1, it);
67+
Single.prototype.append = function (iterable) {
68+
return _from_by_filling(this.M, iterable).cons(this.a);
7469
};
7570

7671
Single.prototype.concat = function (other) {

src/3-tree/implementations/2-Deep.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import _prepend_small_list from '../../0-core/_fast/_prepend_small_list.js';
77
import _fill_right from '../../0-core/_fast/_fill_right.js';
88
import isSameMeasure from '../../_debug/isSameMeasure.js';
99
import _append_small_list from '../../0-core/_fast/_append_small_list.js';
10+
import _from_by_filling from '../../0-core/_fast/_from_by_filling.js';
1011
import {Empty} from './0-Empty.js';
1112
import {Single} from './1-Single.js';
1213

@@ -119,6 +120,10 @@ Deep.prototype._UNSAFE_push = function (value) {
119120
return this._right._UNSAFE_push(this, value);
120121
};
121122

123+
Deep.prototype.prepend = function (iterable) {
124+
return _from_by_filling(this.M, iterable).concat(this);
125+
};
126+
122127
Deep.prototype.append = function (iterable) {
123128
const it = iterable[Symbol.iterator]();
124129

0 commit comments

Comments
 (0)