Skip to content

Commit ebbc2bb

Browse files
nigrosimoneTony133
andauthored
perf: format date/date-time/time without an intermediate Date (#865)
asDate and asTime allocated a second Date shifted by the timezone offset, rendered a full ISO string and sliced out the part they needed. Read the components directly and assemble them with a '00'..'99' lookup table instead. Years outside 0-9999 and invalid Dates still go through toISOString(), so the expanded-year notation and the RangeError are unchanged. Also fixes two asTime edge cases: slice(11, 19) assumed the 24 character ISO form, so +275760-09-12T23:59:59.999Z gave "12T23:59", and the shifted Date could overflow the valid range near the minimum and maximum Date in zones away from UTC, throwing for a valid input. asDate still throws there. Behavior change: getTimezoneOffset() returns whole minutes, so in zones whose offset has a seconds component (LMT, or e.g. Africa/Monrovia before 1972) date and time now render the true local value. 0001-01-01T12:00:00Z in Europe/Rome is "12:49:56" rather than "12:49:00", and the date can move by one day around midnight. End-to-end stringify benchmark: formatted date-time 1,122,372 -> 3,405,844 ops/sec formatted date 880,340 -> 7,797,747 ops/sec formatted time 914,666 -> 8,873,150 ops/sec The serializer-only benchmark shows 4.5x on date-time and 18-24x on date and time. Verified locally on Node 24 and 26: tests, 100% coverage and lint pass. Old and new output compared under UTC, Europe/Rome, America/New_York, Asia/Kolkata and Pacific/Chatham. Tests cover year and millisecond padding, the expanded-year fallback and the RangeError for invalid Dates across date-time, date and time. Co-authored-by: Antonio Tripodi <Tony133@users.noreply.github.com>
1 parent 59cb4cf commit ebbc2bb

2 files changed

Lines changed: 142 additions & 3 deletions

File tree

‎lib/serializer.js‎

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,23 @@
33
// eslint-disable-next-line
44
const STR_ESCAPE = /[\u0000-\u001f\u0022\u005c\ud800-\udfff]/
55

6+
// '00' ... '99', so date parts never need padding at serialization time
7+
const TWO_DIGITS = new Array(100)
8+
for (let i = 0; i < 100; i++) {
9+
TWO_DIGITS[i] = (i < 10 ? '0' : '') + i
10+
}
11+
12+
// Zero-pads a year to 4 digits, or returns null when it falls outside the range
13+
// ISO 8601 writes without the expanded '+YYYYYY' notation. Callers fall back to
14+
// toISOString() for those.
15+
function asYear (year) {
16+
if (year >= 1000) return year <= 9999 ? '' + year : null
17+
if (year >= 100) return '0' + year
18+
if (year >= 10) return '00' + year
19+
if (year >= 0) return '000' + year
20+
return null
21+
}
22+
623
function asString (str) {
724
const len = str.length
825
if (len === 0) {
@@ -97,7 +114,16 @@ module.exports = class Serializer {
97114
asDateTime (date) {
98115
if (date === null) return '""'
99116
if (date instanceof Date) {
100-
return '"' + date.toISOString() + '"'
117+
const year = asYear(date.getUTCFullYear())
118+
if (year === null) return '"' + date.toISOString() + '"'
119+
const ms = date.getUTCMilliseconds()
120+
return '"' + year +
121+
'-' + TWO_DIGITS[date.getUTCMonth() + 1] +
122+
'-' + TWO_DIGITS[date.getUTCDate()] +
123+
'T' + TWO_DIGITS[date.getUTCHours()] +
124+
':' + TWO_DIGITS[date.getUTCMinutes()] +
125+
':' + TWO_DIGITS[date.getUTCSeconds()] +
126+
'.' + (ms < 10 ? '00' : ms < 100 ? '0' : '') + ms + 'Z"'
101127
}
102128
if (typeof date === 'string') {
103129
return asString(date)
@@ -108,7 +134,11 @@ module.exports = class Serializer {
108134
asDate (date) {
109135
if (date === null) return '""'
110136
if (date instanceof Date) {
111-
return '"' + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(0, 10) + '"'
137+
const year = asYear(date.getFullYear())
138+
if (year === null) {
139+
return '"' + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(0, 10) + '"'
140+
}
141+
return '"' + year + '-' + TWO_DIGITS[date.getMonth() + 1] + '-' + TWO_DIGITS[date.getDate()] + '"'
112142
}
113143
if (typeof date === 'string') {
114144
return asString(date)
@@ -119,7 +149,12 @@ module.exports = class Serializer {
119149
asTime (date) {
120150
if (date === null) return '""'
121151
if (date instanceof Date) {
122-
return '"' + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(11, 19) + '"'
152+
const hours = TWO_DIGITS[date.getHours()]
153+
// undefined only for an invalid Date; let toISOString() raise as before
154+
if (hours === undefined) {
155+
return '"' + new Date(date.getTime() - (date.getTimezoneOffset() * 60000)).toISOString().slice(11, 19) + '"'
156+
}
157+
return '"' + hours + ':' + TWO_DIGITS[date.getMinutes()] + ':' + TWO_DIGITS[date.getSeconds()] + '"'
123158
}
124159
if (typeof date === 'string') {
125160
return asString(date)

‎test/date.test.js‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,110 @@ test('verify padding for rendered date in a string when format is time', (t) =>
200200
t.assert.ok(validate(JSON.parse(output)), 'valid schema')
201201
})
202202

203+
test('render a time in a string when the year needs the ISO 8601 expanded notation', (t) => {
204+
t.plan(3)
205+
206+
const schema = {
207+
title: 'a date in a string',
208+
type: 'string',
209+
format: 'time'
210+
}
211+
const stringify = build(schema)
212+
213+
// Years outside 0-9999 render as '+275760-09-12T23:59:59.999Z', which is
214+
// longer than the 24 character form the time used to be sliced out of.
215+
t.assert.equal(stringify(new Date(8640000000000000 - 1)), '"23:59:59"')
216+
t.assert.equal(stringify(new Date(8640000000000000)), '"00:00:00"')
217+
t.assert.equal(stringify(new Date(-8640000000000000)), '"00:00:00"')
218+
})
219+
220+
// Builds a UTC date with an arbitrary year; the Date constructor maps years
221+
// 0-99 to 1900-1999, so the year is set explicitly afterwards.
222+
function dateWithYear (year, month = 0, day = 1, hours = 0, minutes = 0, seconds = 0, ms = 0) {
223+
const date = new Date(Date.UTC(2000, month, day, hours, minutes, seconds, ms))
224+
date.setUTCFullYear(year)
225+
return date
226+
}
227+
228+
test('verify year and millisecond padding when format is date-time', (t) => {
229+
t.plan(5)
230+
231+
const schema = {
232+
title: 'a date in a string',
233+
type: 'string',
234+
format: 'date-time'
235+
}
236+
const stringify = build(schema)
237+
238+
t.assert.equal(stringify(dateWithYear(0)), '"0000-01-01T00:00:00.000Z"')
239+
t.assert.equal(stringify(dateWithYear(1, 1, 3, 4, 5, 6, 7)), '"0001-02-03T04:05:06.007Z"')
240+
t.assert.equal(stringify(dateWithYear(99, 11, 31, 23, 59, 59, 99)), '"0099-12-31T23:59:59.099Z"')
241+
t.assert.equal(stringify(dateWithYear(999, 5, 15, 12, 0, 0, 0)), '"0999-06-15T12:00:00.000Z"')
242+
t.assert.equal(stringify(dateWithYear(9999, 11, 31, 23, 59, 59, 999)), '"9999-12-31T23:59:59.999Z"')
243+
})
244+
245+
test('verify year padding when format is date', (t) => {
246+
t.plan(4)
247+
248+
const schema = {
249+
title: 'a date in a string',
250+
type: 'string',
251+
format: 'date'
252+
}
253+
const stringify = build(schema)
254+
255+
t.assert.equal(stringify(dateWithYear(0)), '"0000-01-01"')
256+
t.assert.equal(stringify(dateWithYear(1, 1, 3)), '"0001-02-03"')
257+
t.assert.equal(stringify(dateWithYear(99, 11, 31)), '"0099-12-31"')
258+
t.assert.equal(stringify(dateWithYear(999, 5, 15)), '"0999-06-15"')
259+
})
260+
261+
test('render a date-time in a string when the year needs the ISO 8601 expanded notation', (t) => {
262+
t.plan(4)
263+
264+
const schema = {
265+
title: 'a date in a string',
266+
type: 'string',
267+
format: 'date-time'
268+
}
269+
const stringify = build(schema)
270+
271+
// Years outside 0-9999 keep the '+YYYYYY' / '-YYYYYY' form of toISOString()
272+
t.assert.equal(stringify(dateWithYear(-1)), '"-000001-01-01T00:00:00.000Z"')
273+
t.assert.equal(stringify(dateWithYear(10000)), '"+010000-01-01T00:00:00.000Z"')
274+
t.assert.equal(stringify(new Date(8640000000000000)), '"+275760-09-13T00:00:00.000Z"')
275+
t.assert.equal(stringify(new Date(-8640000000000000)), '"-271821-04-20T00:00:00.000Z"')
276+
})
277+
278+
test('render a date in a string when the year needs the ISO 8601 expanded notation', (t) => {
279+
t.plan(4)
280+
281+
const schema = {
282+
title: 'a date in a string',
283+
type: 'string',
284+
format: 'date'
285+
}
286+
const stringify = build(schema)
287+
288+
// Years outside 0-9999 keep the pre-existing toISOString().slice(0, 10) output
289+
for (const date of [dateWithYear(-1), dateWithYear(10000), new Date(8640000000000000), new Date(-8640000000000000)]) {
290+
t.assert.equal(stringify(date), JSON.stringify(date.toISOString().slice(0, 10)))
291+
}
292+
})
293+
294+
test('throw a RangeError for an invalid Date when format is date-time, date or time', (t) => {
295+
t.plan(3)
296+
297+
for (const format of ['date-time', 'date', 'time']) {
298+
const stringify = build({
299+
title: 'a date in a string',
300+
type: 'string',
301+
format
302+
})
303+
t.assert.throws(() => stringify(new Date(NaN)), RangeError)
304+
}
305+
})
306+
203307
test('render a nested object in a string when type is date-format as ISOString', (t) => {
204308
t.plan(2)
205309

0 commit comments

Comments
 (0)