From f83534e86b7db495eecb1499806c83862b450bf7 Mon Sep 17 00:00:00 2001 From: uzlopak Date: Wed, 28 Feb 2024 01:35:06 +0100 Subject: [PATCH] perf: improve toIMFDate --- benchmarks/cookies/to-imf-date.mjs | 12 +++++++++++ lib/web/cookies/util.js | 33 +++++++++++++----------------- test/cookie/to-imf-date.js | 21 +++++++++++++++++++ 3 files changed, 47 insertions(+), 19 deletions(-) create mode 100644 benchmarks/cookies/to-imf-date.mjs create mode 100644 test/cookie/to-imf-date.js diff --git a/benchmarks/cookies/to-imf-date.mjs b/benchmarks/cookies/to-imf-date.mjs new file mode 100644 index 00000000000..7934cc74462 --- /dev/null +++ b/benchmarks/cookies/to-imf-date.mjs @@ -0,0 +1,12 @@ +import { bench, group, run } from 'mitata' +import { toIMFDate } from '../../lib/web/cookies/util.js' + +const date = new Date() + +group('toIMFDate', () => { + bench(`toIMFDate: ${date}`, () => { + return toIMFDate(date) + }) +}) + +await run() diff --git a/lib/web/cookies/util.js b/lib/web/cookies/util.js index 6d3a79b69ad..03432431e86 100644 --- a/lib/web/cookies/util.js +++ b/lib/web/cookies/util.js @@ -114,6 +114,18 @@ function validateCookieDomain (domain) { } } +const IMFDays = [ + 'Sun', 'Mon', 'Tue', 'Wed', + 'Thu', 'Fri', 'Sat' +] + +const IMFMonths = [ + 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', + 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' +] + +const IMFPaddedNumbers = Array(61).fill(0).map((_, i) => i.toString().padStart(2, '0')) + /** * @see https://www.rfc-editor.org/rfc/rfc7231#section-7.1.1.1 * @param {number|Date} date @@ -160,25 +172,7 @@ function toIMFDate (date) { date = new Date(date) } - const days = [ - 'Sun', 'Mon', 'Tue', 'Wed', - 'Thu', 'Fri', 'Sat' - ] - - const months = [ - 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', - 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' - ] - - const dayName = days[date.getUTCDay()] - const day = date.getUTCDate().toString().padStart(2, '0') - const month = months[date.getUTCMonth()] - const year = date.getUTCFullYear() - const hour = date.getUTCHours().toString().padStart(2, '0') - const minute = date.getUTCMinutes().toString().padStart(2, '0') - const second = date.getUTCSeconds().toString().padStart(2, '0') - - return `${dayName}, ${day} ${month} ${year} ${hour}:${minute}:${second} GMT` + return `${IMFDays[date.getUTCDay()]}, ${IMFPaddedNumbers[date.getUTCDate()]} ${IMFMonths[date.getUTCMonth()]} ${date.getUTCFullYear()} ${IMFPaddedNumbers[date.getUTCHours()]}:${IMFPaddedNumbers[date.getUTCMinutes()]}:${IMFPaddedNumbers[date.getUTCSeconds()]} GMT` } /** @@ -287,6 +281,7 @@ function getHeadersList (headers) { module.exports = { isCTLExcludingHtab, + toIMFDate, stringify, getHeadersList } diff --git a/test/cookie/to-imf-date.js b/test/cookie/to-imf-date.js new file mode 100644 index 00000000000..3e48360919d --- /dev/null +++ b/test/cookie/to-imf-date.js @@ -0,0 +1,21 @@ +'use strict' + +const { test, describe } = require('node:test') +const { strictEqual } = require('node:assert') + +const { + toIMFDate +} = require('../../lib/web/cookies/util') + +describe('toIMFDate', () => { + test('should return the same as Date.prototype.toGMTString()', () => { + for (let i = 1; i <= 1e6; i *= 2) { + const date = new Date(i) + strictEqual(toIMFDate(date), date.toGMTString()) + } + for (let i = 0; i <= 1e6; i++) { + const date = new Date(Math.trunc(Math.random() * 8640000000000000)) + strictEqual(toIMFDate(date), date.toGMTString()) + } + }) +})