Skip to content

Instantly share code, notes, and snippets.

@asalant
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save asalant/6dc3c15ee6a20d44db1f to your computer and use it in GitHub Desktop.

Select an option

Save asalant/6dc3c15ee6a20d44db1f to your computer and use it in GitHub Desktop.

Revisions

  1. asalant revised this gist Apr 23, 2015. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion output1
    Original file line number Diff line number Diff line change
    @@ -15,4 +15,5 @@ No reviver: 179ms
    Regex reviver: 284ms
    Regex reviver optimized: 258ms

    So most of the time is spent actually converting the string to Date, not checking to see if it looks like a Date.
    So most of the time is spent actually converting the string to Date, not
    checking to see if it looks like a Date.
  2. asalant revised this gist Apr 23, 2015. 1 changed file with 7 additions and 2 deletions.
    9 changes: 7 additions & 2 deletions output1
    Original file line number Diff line number Diff line change
    @@ -2,9 +2,14 @@ No reviver: 148ms
    Regex reviver: 633ms
    Regex reviver optimized: 917ms

    Surprising to see that the additional length check in isIsoDateOptimized actually slows this down. The regex engine in node must be doing this kind of optimization already faster than JavaScript.
    Surprising to see that the additional length check in isIsoDateOptimized
    actually slows this down. The regex engine in node must be doing this
    kind of optimization already faster than JavaScript.

    Some of the time here is spent in new Date(). If we assume that consuming code of the parsed JSON would be doing that conversion of string to Date also, we should get rid of it in this test. Changing lines 16-17 to just `return value` results in the following:
    Some of the time here is spent in new Date(). If we assume that consuming
    code of the parsed JSON would be doing that conversion of string to Date
    also, we should get rid of it in this test. Changing lines 16-17 to just
    `return value` results in the following:

    No reviver: 179ms
    Regex reviver: 284ms
  3. asalant created this gist Apr 23, 2015.
    38 changes: 38 additions & 0 deletions date_reviver_perf.coffee
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    isoDateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z$/

    isIsoDate = (value) ->
    return value unless typeof(value) is 'string'

    isoDateRegex.test(value)

    isIsoDateOptimized = (value) ->
    return value unless typeof(value) is 'string'
    return value unless value.length is 24

    isoDateRegex.test(value)

    dateReviver = (dateTestFn) ->
    (key, value) ->
    return value unless dateTestFn(value)
    return new Date(value)

    data = for i in [1..10000]
    date1: new Date(Date.now() + i * 1000)
    number: 1
    string1: "Hello there"
    string2: "Here's a slightly longer string."
    obj: { a: 1, b: "2", c: '3'}

    json = JSON.stringify(data)

    startAt = Date.now()
    JSON.parse(json) for i in [1..10]
    console.log "No reviver: #{Date.now() - startAt}ms"#, parsed

    startAt = Date.now()
    JSON.parse(json, dateReviver(isIsoDate)) for i in [1..10]
    console.log "Regex reviver: #{Date.now() - startAt}ms"#, parsed

    startAt = Date.now()
    JSON.parse(json, dateReviver(isIsoDateOptimized)) for i in [1..10]
    console.log "Regex reviver optimized: #{Date.now() - startAt}ms"#, parsed
    13 changes: 13 additions & 0 deletions output1
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,13 @@
    No reviver: 148ms
    Regex reviver: 633ms
    Regex reviver optimized: 917ms

    Surprising to see that the additional length check in isIsoDateOptimized actually slows this down. The regex engine in node must be doing this kind of optimization already faster than JavaScript.

    Some of the time here is spent in new Date(). If we assume that consuming code of the parsed JSON would be doing that conversion of string to Date also, we should get rid of it in this test. Changing lines 16-17 to just `return value` results in the following:

    No reviver: 179ms
    Regex reviver: 284ms
    Regex reviver optimized: 258ms

    So most of the time is spent actually converting the string to Date, not checking to see if it looks like a Date.