Skip to content

Commit d292718

Browse files
giacomocavalierilpil
authored andcommitted
improve code generate for list pattern matching on js
1 parent 504f20b commit d292718

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
be byte aligned, and the `bits` segment type is now supported in patterns.
2525
([Richard Viney](https://github.com/richard-viney))
2626

27+
- The code generated for list pattern matching on the JavaScript target is now
28+
more efficient. Gleam code that relies heavily on list pattern matching can
29+
now be up to twice as fast.
30+
([yoshi~](https://github.com/yoshi-monster))
31+
2732
### Build tool
2833

2934
- The build tool now supports Git dependencies. For example:

compiler-core/templates/prelude.mjs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,27 @@ export class List {
2929

3030
// @internal
3131
atLeastLength(desired) {
32-
for (let _ of this) {
33-
if (desired <= 0) return true;
34-
desired--;
35-
}
36-
return desired <= 0;
32+
let current = this;
33+
while (desired-- > 0 && current) current = current.tail;
34+
return current !== undefined;
3735
}
3836

3937
// @internal
4038
hasLength(desired) {
41-
for (let _ of this) {
42-
if (desired <= 0) return false;
43-
desired--;
44-
}
45-
return desired === 0;
39+
let current = this;
40+
while (desired-- > 0 && current) current = current.tail;
41+
return desired === -1 && current instanceof Empty;
4642
}
4743

4844
// @internal
4945
countLength() {
46+
let current = this;
5047
let length = 0;
51-
for (let _ of this) length++;
52-
return length;
48+
while (current) {
49+
current = current.tail;
50+
length++;
51+
}
52+
return length - 1;
5353
}
5454
}
5555

0 commit comments

Comments
 (0)