N
N
Nikolay372019-06-05 12:12:02
JavaScript
Nikolay37, 2019-06-05 12:12:02

Parsing JSON data to JS, how faster?

I receive json data from a websocket, parse it and set a condition, can I use some plugins or other additives to make this process faster (I mean speed up data parsing), or is native functions the fastest thing that can be?
In fact, as I understand it, the native ones should be the fastest, because. they do according to the 1st condition, the rest of the plugins will do several actions

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
sim3x, 2019-06-05
@Nikolay37

native functions is the fastest that can be?
Yes

G
grinat, 2019-06-05
@grinat

const json = `{
      "type": "widget-chart",
      "title": "Handcrafted Wooden Sausages",
      "data": {
        "chart": {
          "height": "100%",
          "type": "donut"
        },
        "series": [
          44,
          55,
          41,
          17,
          15
        ],
        "responsive": [
          {
            "breakpoint": 480,
            "options": {
              "chart": {
                "width": 200
              },
              "legend": {
                "position": "bottom"
              }
            }
          }
        ]
      },
      "_id": "5cf6307a245e2b001efd2111",
      "id": "5cf6307a245e2b001efd2111"
    }
`

console.time('eval')
let e = {}
eval('e = ' + json)
console.timeEnd('eval')


console.time('JSON.parse')
let p = {}
p = JSON.parse(json)
console.timeEnd('JSON.parse')


console.time('Function')
let f = {}
f = new Function('return ' + json)
console.timeEnd('Function')

eval: 0.209ms
JSON.parse: 0.046ms
Function: 0.085ms

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question