Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cookies: improve perf of toIMFDate #2867

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions benchmarks/cookies/to-imf-date.mjs
Original file line number Diff line number Diff line change
@@ -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()
33 changes: 14 additions & 19 deletions lib/web/cookies/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`
}

/**
Expand Down Expand Up @@ -287,6 +281,7 @@ function getHeadersList (headers) {

module.exports = {
isCTLExcludingHtab,
toIMFDate,
stringify,
getHeadersList
}
21 changes: 21 additions & 0 deletions test/cookie/to-imf-date.js
Original file line number Diff line number Diff line change
@@ -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())
}
})
})
Loading