Home  >  Q&A  >  body text

javascript - Whether string exists for a piece of data performs better than object

When developing small programs or some frameworks, many request callbacks are string. Is it because string has better performance than object and json?

滿天的星座滿天的星座2726 days ago877

reply all(2)I'll reply

  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-05-24 11:34:23

    Because. . No matter what, the background response is a string. So js 在收到 object 的时候,也是一串 json 的字符串。然后,js needs to parse this string into an object. The performance is definitely better than that of an ordinary string, but the performance advantage is almost negligible. After all, if there are complex data structures, ordinary strings cannot solve the needs.

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-24 11:34:23

    The backend cannot be done直接Object.

    The reason is very simple: there are various back-end languages, and the various Objects sent may not be treated directly as JS objects by the front-end JS.

    So we need to use some kind of object representation 标准范式 to convert different types of objects into objects that JS can understand.

    Perhaps you have guessed that this paradigm is JSON ———— an object representation (in string form)

    In this way, any backend language can convert its object into JSON (with the help of a library or writing it yourself) and then send it to the front end, so that the front end can easily convert JSON into JS objects.

    So you can see:

    Different implementations of Object in multiple languages ​​=> Obejct 实现 => JSON => JavaScript => JavaScript objects

    The same goes for vice versa.

    Of course, there is not only JSON, a standard paradigm, but also many (xml, etc.)


    Or you can think differently:

    Backstage Object 调制成 JSON字符串 然后发往前端。
    前端收到后 解调成 JS Object


    As for the performance you mentioned, generally string operations are much faster than some operations on objects.

    However, objects have hierarchical inclusion relationships and multiple data types, which string parameters do not have.

    reply
    0
  • Cancelreply