Skip to content

Commit 6a92595

Browse files
committed
Optimize get method
1 parent 461604d commit 6a92595

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
In next release ...
22

3+
- Use lookup table to optimize `get` method.
4+
35
- The `connect` method now returns a boolean status of whether the
46
connection is encrypted.
57

src/result.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,25 @@ type ResultHandler = (resolve: Resolver) => void;
88
type Callback<T> = (item: T) => void;
99

1010
export class ResultRow<T> {
11-
private readonly length: number;
11+
private readonly lookup: {[name: string]: number};
1212

1313
constructor(public readonly names: string[], public readonly data: T[]) {
14-
this.length = names.length;
14+
const lookup: {[name: string]: number} = {};
15+
let i = 0;
16+
for (const name of names) {
17+
lookup[name] = i;
18+
i++;
19+
}
20+
this.lookup = lookup;
1521
}
1622

1723
[Symbol.iterator](): Iterator<T> {
18-
return this.data[Symbol.iterator]();
24+
return this.data[Symbol.iterator]();
1925
}
2026

2127
get(name: string): T | undefined {
22-
for (let i = 0; i < this.length; i++) {
23-
if (this.names[i] === name) {
24-
return this.data[i];
25-
}
26-
}
28+
const i = this.lookup[name];
29+
return this.data[i];
2730
}
2831
}
2932

0 commit comments

Comments
 (0)