1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-04 15:02:09 +03:00

improve encodeCommand performance, add set-get-delete-string benchmark

This commit is contained in:
leibale
2021-12-01 20:17:52 -05:00
parent cba0289ff7
commit f55de0efbf
10 changed files with 916 additions and 8 deletions

View File

@@ -92,14 +92,33 @@ export function transformCommandArguments<T = unknown>(
const DELIMITER = '\r\n';
export function* encodeCommand(args: RedisCommandArguments): IterableIterator<string | Buffer> {
yield `*${args.length}${DELIMITER}`;
let strings = `*${args.length}${DELIMITER}`,
stringsLength = 0;
for (const arg of args) {
const byteLength = typeof arg === 'string' ? Buffer.byteLength(arg): arg.length;
yield `$${byteLength.toString()}${DELIMITER}`;
yield arg;
yield DELIMITER;
const isString = typeof arg === 'string',
byteLength = isString ? Buffer.byteLength(arg): arg.length;
strings += `$${byteLength}${DELIMITER}`;
if (isString) {
const totalLength = stringsLength + byteLength;
if (totalLength > 1024) {
yield strings;
strings = arg;
stringsLength = byteLength;
} else {
strings += arg;
stringsLength = totalLength;
}
} else {
yield strings;
stringsLength = 0;
yield arg;
}
strings += DELIMITER;
}
yield strings;
}
export function transformCommandReply(