Skip to content

Commit a64516e

Browse files
authored
Merge pull request #81 from malthe/lookup-table
Optimize "get" method
2 parents 109f19f + 6a92595 commit a64516e

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
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/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ export type ResultIterator = _ResultIterator<Value>;
5050

5151
export type ResultRow = _ResultRow<Value>;
5252

53-
export type Connect = (Error | string | null);
53+
export type Connect = Error | null;
5454

5555
export type End = void;
5656

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

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"allowSyntheticDefaultImports": true,
1616
"experimentalDecorators": true,
1717
"emitDecoratorMetadata": true,
18+
"noImplicitAny": true,
1819
"noUnusedParameters": true,
1920
"noUnusedLocals": true,
2021
"declarationDir": "dist",

0 commit comments

Comments
 (0)