Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/sqlite-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ export function Factory(Module) {
const tmp = Module._malloc(8);
const tmpPtr = [tmp, tmp + 4];

const textEncoder = new TextEncoder();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This is a good optimization! Initializing TextEncoder once and reusing it avoids unnecessary object creation. Consider adding a comment explaining why this is being done, referencing the performance benefits.

  // Initialize TextEncoder once for performance to avoid repeated object creation
  const textEncoder = new TextEncoder();

// Convert a JS string to a C string. sqlite3_malloc is used to allocate
// memory (use sqlite3_free to deallocate).
function createUTF8(s) {
if (typeof s !== 'string') return 0;
const utf8 = new TextEncoder().encode(s);
const utf8 = textEncoder.encode(s);
const zts = Module._sqlite3_malloc(utf8.byteLength + 1);
Module.HEAPU8.set(utf8, zts);
Module.HEAPU8[zts + utf8.byteLength] = 0;
Expand Down Expand Up @@ -661,7 +662,7 @@ export function Factory(Module) {
const onFinally = [];
try {
// Encode SQL string to UTF-8.
const utf8 = new TextEncoder().encode(sql);
const utf8 = textEncoder.encode(sql);

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