Skip to content

Commit dbcc7dd

Browse files
committed
Add comment about switch/case being more performant than lookup table, possibly due to function inlining by the engine
1 parent 81c4b97 commit dbcc7dd

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

benchmarks/benchmarks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const suite = new Benchmark.Suite();
1919

2020
// add tests
2121
suite
22-
.add('autolinker@current', runAutolinkerCurrent)
22+
.add('autolinker@current-src', runAutolinkerCurrent)
2323
.add('[email protected]', runAutolinker4_1_2)
2424
// .add('[email protected]', runAutolinker4_1_1)
2525
// .add('[email protected]', runAutolinker4_1_0)

src/parser/parse-matches.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ export function parseMatches(text: string, args: ParseMatchesArgs): Match[] {
9898
for (let stateIdx = context.stateMachines.length - 1; stateIdx >= 0; stateIdx--) {
9999
const stateMachine = context.stateMachines[stateIdx];
100100

101+
// Performance note: tried turning this switch/case into a lookup
102+
// table, but resulted in ~350 fewer ops/sec in benchmarks.
103+
// Switch/case may be more efficient due to potential function
104+
// inlining by the engine? Either way, run benchmarks if
105+
// attempting to change again.
101106
switch (stateMachine.state) {
102107
// Protocol-relative URL states
103108
case State.ProtocolRelativeSlash1:
@@ -137,7 +142,7 @@ export function parseMatches(text: string, args: ParseMatchesArgs): Match[] {
137142
stateIpV4Digit(context, stateMachine as IpV4UrlStateMachine, char);
138143
break;
139144
case State.IpV4Dot:
140-
stateIPv4Dot(context, stateMachine as IpV4UrlStateMachine, char);
145+
stateIpV4Dot(context, stateMachine as IpV4UrlStateMachine, char);
141146
break;
142147

143148
case State.PortColon:
@@ -556,7 +561,7 @@ function stateIpV4Digit(
556561
}
557562
}
558563

559-
function stateIPv4Dot(
564+
function stateIpV4Dot(
560565
context: ParseMatchesContext,
561566
stateMachine: IpV4UrlStateMachine,
562567
char: string

0 commit comments

Comments
 (0)