From 1dc1a86f256031166384660b982a4d8ffc5d5202 Mon Sep 17 00:00:00 2001 From: Ayoub-Mabrouk Date: Fri, 8 Nov 2024 22:03:13 +0100 Subject: [PATCH] Refactor parseTokenList function for optimization - Replaced substring with slice for clearer token extraction. - Simplified token parsing by combining space, comma, and end-of-string checks in a single condition. - Removed unnecessary end tracking variable, utilizing index directly for efficiency. - Streamlined the token list generation by combining the logic for handling the final token. --- index.js | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index fc3dea7..cf3653f 100644 --- a/index.js +++ b/index.js @@ -103,34 +103,24 @@ function parseHttpDate (date) { * Parse a HTTP token list. * * @param {string} str + * @returns {Array} * @private */ function parseTokenList (str) { - var end = 0 var list = [] var start = 0 + var len = str.length - // gather tokens - for (var i = 0, len = str.length; i < len; i++) { - switch (str.charCodeAt(i)) { - case 0x20: /* */ - if (start === end) { - start = end = i + 1 - } - break - case 0x2c: /* , */ - list.push(str.substring(start, end)) - start = end = i + 1 - break - default: - end = i + 1 - break + for (var i = 0; i <= len; i++) { + var charCode = str.charCodeAt(i) + if (charCode === 0x20 || charCode === 0x2c || i === len) { + if (i > start) { + list.push(str.slice(start, i)) + } + start = i + 1 } } - // final token - list.push(str.substring(start, end)) - return list }