Skip to content

Commit ec25083

Browse files
committed
use if else instead of switch
1 parent 16591cf commit ec25083

3 files changed

Lines changed: 16 additions & 13 deletions

File tree

src/jit.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,21 @@ export const router_compile_to_code = (
1313

1414
if (matchAllIdx !== -1) linear_map_swap(router, matchAllIdx, trees.length - 1);
1515

16-
let str = `switch(${methodId}){`;
16+
let str = '';
1717
for (
18-
let i = 0, methods = router[0], treesLen = trees.length - (matchAllIdx !== -1 ? 1 : 0);
18+
let i = 0,
19+
methods = router[0],
20+
treesLen = trees.length - (matchAllIdx !== -1 ? 1 : 0),
21+
prefix = `if(${methodId}===`;
1922
i < treesLen;
2023
i++
21-
)
24+
) {
2225
str +=
23-
`case${JSON.stringify(methods[i])}:` +
24-
tree_compile_to_code(trees[i], resultId, pathId) +
25-
'break;';
26-
str += '}';
26+
prefix +
27+
JSON.stringify(methods[i]) +
28+
`){${tree_compile_to_code(trees[i], resultId, pathId)}}`;
29+
if (i === 0) prefix = 'else ' + prefix;
30+
}
2731

2832
return matchAllIdx !== -1
2933
? str + tree_compile_to_code(trees[trees.length - 1], resultId, pathId)

src/tree/jit.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ export const tree_compile_to_code = (
1010
keys = tree[0],
1111
values = tree[1];
1212

13-
if (keys.length > 0) {
14-
str = `switch(${pathId}){`;
15-
for (let i = 0; i < keys.length; i++) str += `case${JSON.stringify(keys[i])}:{${values[i]}}`;
16-
str += '}';
17-
}
13+
if (keys.length > 0)
14+
for (let i = 0, prefix = `if(${pathId}===`; i < keys.length; i++) {
15+
str += prefix + JSON.stringify(keys[i]) + `){${values[i]}}`;
16+
if (i === 0) prefix = 'else ' + prefix;
17+
}
1818

1919
if (tree[2] !== null) {
2020
// console.log(JSON.stringify(tree[2]!, null, 2));

tests/tree/jit.test.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)