Skip to content

Commit e087f36

Browse files
committed
Make Stdlib.Dict more performant
1 parent 66723ba commit e087f36

File tree

3 files changed

+57
-42
lines changed

3 files changed

+57
-42
lines changed

lib/es6/Stdlib_Dict.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@ function $$delete$1(dict, string) {
55
delete(dict[string]);
66
}
77

8-
function forEach(dict, f) {
9-
Object.values(dict).forEach(value => f(value));
10-
}
8+
let forEach = ((dict, f) => {
9+
for (var key in dict) {
10+
f(dict[key]);
11+
}
12+
});
1113

12-
function forEachWithKey(dict, f) {
13-
Object.keys(dict).forEach(key => f(dict[key], key));
14-
}
14+
let forEachWithKey = ((dict, f) => {
15+
for (var key in dict) {
16+
f(dict[key], key);
17+
}
18+
});
1519

16-
function mapValues(dict, f) {
17-
let target = {};
18-
Object.keys(dict).forEach(key => {
19-
let value = dict[key];
20-
target[key] = f(value);
20+
let mapValues = ((dict, f) => {
21+
var target = {};
22+
for (var key in dict) {
23+
target[key] = f(dict[key]);
24+
}
25+
return target;
2126
});
22-
return target;
23-
}
2427

2528
export {
2629
$$delete$1 as $$delete,

lib/js/Stdlib_Dict.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@ function $$delete$1(dict, string) {
55
delete(dict[string]);
66
}
77

8-
function forEach(dict, f) {
9-
Object.values(dict).forEach(value => f(value));
10-
}
8+
let forEach = ((dict, f) => {
9+
for (var key in dict) {
10+
f(dict[key]);
11+
}
12+
});
1113

12-
function forEachWithKey(dict, f) {
13-
Object.keys(dict).forEach(key => f(dict[key], key));
14-
}
14+
let forEachWithKey = ((dict, f) => {
15+
for (var key in dict) {
16+
f(dict[key], key);
17+
}
18+
});
1519

16-
function mapValues(dict, f) {
17-
let target = {};
18-
Object.keys(dict).forEach(key => {
19-
let value = dict[key];
20-
target[key] = f(value);
20+
let mapValues = ((dict, f) => {
21+
var target = {};
22+
for (var key in dict) {
23+
target[key] = f(dict[key]);
24+
}
25+
return target;
2126
});
22-
return target;
23-
}
2427

2528
exports.$$delete = $$delete$1;
2629
exports.forEach = forEach;

runtime/Stdlib_Dict.res

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,31 @@ let delete = (dict, string) => {
2424

2525
@val external copy: (@as(json`{}`) _, dict<'a>) => dict<'a> = "Object.assign"
2626

27-
let forEach = (dict, f) => {
28-
dict->valuesToArray->Stdlib_Array.forEach(value => f(value))
29-
}
30-
31-
@inline
32-
let forEachWithKey = (dict, f) => {
33-
dict->keysToArray->Stdlib_Array.forEach(key => f(dict->getUnsafe(key), key))
34-
}
35-
36-
let mapValues = (dict, f) => {
37-
let target = make()
38-
dict->forEachWithKey((value, key) => {
39-
target->set(key, f(value))
40-
})
41-
target
42-
}
27+
let forEach: (dict<'a>, 'a => unit) => unit = %raw(`
28+
(dict, f) => {
29+
for (var key in dict) {
30+
f(dict[key]);
31+
}
32+
}
33+
`)
34+
35+
let forEachWithKey: (dict<'a>, ('a, string) => unit) => unit = %raw(`
36+
(dict, f) => {
37+
for (var key in dict) {
38+
f(dict[key], key);
39+
}
40+
}
41+
`)
42+
43+
let mapValues: (dict<'a>, 'a => 'b) => dict<'b> = %raw(`
44+
(dict, f) => {
45+
var target = {};
46+
for (var key in dict) {
47+
target[key] = f(dict[key]);
48+
}
49+
return target;
50+
}
51+
`)
4352

4453
external has: (dict<'a>, string) => bool = "%dict_has"
4554

0 commit comments

Comments
 (0)