Skip to content

Commit 8154476

Browse files
authored
Don't use Array.shift in custom deserializers (#1364)
* Don't use Array.shift in custom deserializers 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 using plain indexing to achieve the same thing. * don't use deprecated wrangler command in the ci
1 parent 9846a4b commit 8154476

File tree

6 files changed

+21
-21
lines changed

6 files changed

+21
-21
lines changed

.github/workflows/tests.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ jobs:
316316
working-directory: examples/cloudflare-workers
317317

318318
- name: Deploy
319-
run: wrangler publish
319+
run: wrangler deploy
320320
working-directory: examples/cloudflare-workers
321321
env:
322322
CLOUDFLARE_API_TOKEN: ${{secrets.CF_API_TOKEN}}
@@ -409,7 +409,7 @@ jobs:
409409
working-directory: examples/cloudflare-workers-with-typescript
410410

411411
- name: Deploy
412-
run: wrangler publish
412+
run: wrangler deploy
413413
working-directory: examples/cloudflare-workers-with-typescript
414414
env:
415415
CLOUDFLARE_API_TOKEN: ${{secrets.CF_API_TOKEN}}

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)