Skip to content

Commit

Permalink
perf: improve toIMFDate (#2867)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Feb 28, 2024
1 parent 5600aa1 commit 44e7ed8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
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())
}
})
})

0 comments on commit 44e7ed8

Please sign in to comment.