Skip to content

Commit 32508bb

Browse files
committed
Don't use Array.shift in custom serializers
It is not a O(1) operation in Node.js (V8 engine). So, custom deserializers that were using it extensively like hgetall was running extremely slow on large payloads. We are now use plain indexing to achieve the same thing.
1 parent 9846a4b commit 32508bb

File tree

5 files changed

+19
-19
lines changed

5 files changed

+19
-19
lines changed

pkg/commands/hgetall.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ function deserialize<TData extends Record<string, unknown>>(result: string[]): T
66
return null;
77
}
88
const obj: Record<string, unknown> = {};
9-
while (result.length >= 2) {
10-
const key = result.shift()!;
11-
const value = result.shift()!;
9+
for (let i = 0; i < result.length; i += 2) {
10+
const key = result[i];
11+
const value = result[i + 1];
1212
try {
1313
// handle unsafe integer
1414
const valueIsNumberAndNotSafeInteger =

pkg/commands/hrandfield.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ function deserialize<TData extends Record<string, unknown>>(result: string[]): T
66
return null;
77
}
88
const obj: Record<string, unknown> = {};
9-
while (result.length >= 2) {
10-
const key = result.shift()!;
11-
const value = result.shift()!;
9+
for (let i = 0; i < result.length; i += 2) {
10+
const key = result[i];
11+
const value = result[i + 1];
1212
try {
1313
obj[key] = JSON.parse(value);
1414
} catch {

pkg/commands/xrange.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ function deserialize<TData extends Record<string, Record<string, unknown>>>(
66
): TData {
77
const obj: Record<string, Record<string, unknown>> = {};
88
for (const e of result) {
9-
while (e.length >= 2) {
10-
const streamId = e.shift() as string;
11-
const entries = e.shift()!;
9+
for (let i = 0; i < e.length; i += 2) {
10+
const streamId = e[i] as string;
11+
const entries = e[i + 1];
1212

1313
if (!(streamId in obj)) {
1414
obj[streamId] = {};
1515
}
16-
while (entries.length >= 2) {
17-
const field = (entries as string[]).shift()!;
18-
const value = (entries as string[]).shift()!;
16+
for (let j = 0; j < entries.length; j += 2) {
17+
const field = (entries as string[])[j];
18+
const value = (entries as string[])[j + 1];
1919

2020
try {
2121
obj[streamId][field] = JSON.parse(value);

pkg/commands/xrevrange.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ function deserialize<TData extends Record<string, Record<string, unknown>>>(
2424
): TData {
2525
const obj: Record<string, Record<string, unknown>> = {};
2626
for (const e of result) {
27-
while (e.length >= 2) {
28-
const streamId = e.shift() as string;
29-
const entries = e.shift()!;
27+
for (let i = 0; i < e.length; i += 2) {
28+
const streamId = e[i] as string;
29+
const entries = e[i + 1];
3030

3131
if (!(streamId in obj)) {
3232
obj[streamId] = {};
3333
}
34-
while (entries.length >= 2) {
35-
const field = (entries as string[]).shift()!;
36-
const value = (entries as string[]).shift()!;
34+
for (let j = 0; j < entries.length; j += 2) {
35+
const field = (entries as string[])[j];
36+
const value = (entries as string[])[j + 1];
3737

3838
try {
3939
obj[streamId][field] = JSON.parse(value);

platforms/nodejs.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ export class Redis extends core.Redis {
185185
if (!url) {
186186
console.warn("[Upstash Redis] Unable to find environment variable: `UPSTASH_REDIS_REST_URL`");
187187
}
188-
188+
189189
// @ts-ignore process will be defined in node
190190
const token = process.env.UPSTASH_REDIS_REST_TOKEN || process.env.KV_REST_API_TOKEN;
191191
if (!token) {

0 commit comments

Comments
 (0)