Skip to content

Commit a883e22

Browse files
authored
Merge pull request rhashimoto#274 from grantcox/single-textencoder-instance
Use a single `TextEncoder` instance
2 parents 7d45baf + 848ca54 commit a883e22

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

src/sqlite-api.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ export function Factory(Module) {
3434
const tmp = Module._malloc(8);
3535
const tmpPtr = [tmp, tmp + 4];
3636

37+
const textEncoder = new TextEncoder();
3738
// Convert a JS string to a C string. sqlite3_malloc is used to allocate
3839
// memory (use sqlite3_free to deallocate).
3940
function createUTF8(s) {
4041
if (typeof s !== 'string') return 0;
41-
const utf8 = new TextEncoder().encode(s);
42+
const utf8 = textEncoder.encode(s);
4243
const zts = Module._sqlite3_malloc(utf8.byteLength + 1);
4344
Module.HEAPU8.set(utf8, zts);
4445
Module.HEAPU8[zts + utf8.byteLength] = 0;
@@ -661,7 +662,7 @@ export function Factory(Module) {
661662
const onFinally = [];
662663
try {
663664
// Encode SQL string to UTF-8.
664-
const utf8 = new TextEncoder().encode(sql);
665+
const utf8 = textEncoder.encode(sql);
665666

666667
// Copy encoded string to WebAssembly memory. The SQLite docs say
667668
// zero-termination is a minor optimization so add room for that.

0 commit comments

Comments
 (0)