Skip to content

Commit 28e3a95

Browse files
committed
Minor perf improvements
1 parent aaa7067 commit 28e3a95

File tree

3 files changed

+41
-30
lines changed

3 files changed

+41
-30
lines changed

benchmarks/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ for (let i = 0; i < 1_000_000; i++) {
1414
queries.push(query);
1515

1616
// Compute properties for perf testing.
17+
query.sql;
1718
query.text;
1819
query.values;
1920
}

src/index.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@ describe("sql template tag", () => {
1111
expect(query.values).toEqual([]);
1212
});
1313

14+
it("should embed sql in sql", () => {
15+
const tableName = sql`books`;
16+
const query = sql`SELECT * FROM ${tableName}`;
17+
18+
expect(query.sql).toEqual("SELECT * FROM books");
19+
expect(query.text).toEqual("SELECT * FROM books");
20+
expect(query.values).toEqual([]);
21+
});
22+
1423
it("should store values", () => {
1524
const name = "Blake";
1625
const query = sql`SELECT * FROM books WHERE author = ${name}`;

src/index.ts

+31-30
Original file line numberDiff line numberDiff line change
@@ -20,67 +20,68 @@ export class Sql {
2020
rawStrings: ReadonlyArray<string>,
2121
rawValues: ReadonlyArray<RawValue>
2222
) {
23-
let valuesLength = rawValues.length;
24-
let stringsLength = rawStrings.length;
25-
26-
if (stringsLength === 0) {
27-
throw new TypeError("Expected at least 1 string");
28-
}
23+
if (rawStrings.length - 1 !== rawValues.length) {
24+
if (rawStrings.length === 0) {
25+
throw new TypeError("Expected at least 1 string");
26+
}
2927

30-
if (stringsLength - 1 !== valuesLength) {
3128
throw new TypeError(
32-
`Expected ${stringsLength} strings to have ${stringsLength - 1} values`
29+
`Expected ${rawStrings.length} strings to have ${
30+
rawStrings.length - 1
31+
} values`
3332
);
3433
}
3534

36-
for (const child of rawValues) {
37-
if (child instanceof Sql) {
38-
valuesLength += child.values.length - 1;
39-
stringsLength += child.strings.length - 2;
40-
}
41-
}
35+
const valuesLength = rawValues.reduce<number>(
36+
(len, value) => len + (value instanceof Sql ? value.values.length : 1),
37+
0
38+
);
4239

4340
this.values = new Array(valuesLength);
44-
this.strings = new Array(stringsLength);
41+
this.strings = new Array(valuesLength + 1);
4542

4643
this.strings[0] = rawStrings[0];
4744

4845
// Iterate over raw values, strings, and children. The value is always
4946
// positioned between two strings, e.g. `index + 1`.
50-
let index = 1;
51-
let position = 0;
52-
while (index < rawStrings.length) {
53-
const child = rawValues[index - 1];
54-
const rawString = rawStrings[index++];
47+
let i = 0,
48+
pos = 0;
49+
while (i < rawValues.length) {
50+
const child = rawValues[i++];
51+
const rawString = rawStrings[i];
5552

5653
// Check for nested `sql` queries.
5754
if (child instanceof Sql) {
5855
// Append child prefix text to current string.
59-
this.strings[position] += child.strings[0];
56+
this.strings[pos] += child.strings[0];
6057

6158
let childIndex = 0;
6259
while (childIndex < child.values.length) {
63-
this.values[position++] = child.values[childIndex++];
64-
this.strings[position] = child.strings[childIndex];
60+
this.values[pos++] = child.values[childIndex++];
61+
this.strings[pos] = child.strings[childIndex];
6562
}
6663

6764
// Append raw string to current string.
68-
this.strings[position] += rawString;
65+
this.strings[pos] += rawString;
6966
} else {
70-
this.values[position++] = child;
71-
this.strings[position] = rawString;
67+
this.values[pos++] = child;
68+
this.strings[pos] = rawString;
7269
}
7370
}
7471
}
7572

7673
get text() {
77-
return this.strings.reduce(
78-
(text, part, index) => `${text}$${index}${part}`
79-
);
74+
let i = 1,
75+
value = this.strings[0];
76+
while (i < this.strings.length) value += `$${i}${this.strings[i++]}`;
77+
return value;
8078
}
8179

8280
get sql() {
83-
return this.strings.join("?");
81+
let i = 1,
82+
value = this.strings[0];
83+
while (i < this.strings.length) value += `?${this.strings[i++]}`;
84+
return value;
8485
}
8586

8687
inspect() {

0 commit comments

Comments
 (0)