首页 > 解决方案 > Does sorting JSON keys/attributes alphabetically make a difference to performance

问题描述

If I have a JSON file that let's say represents a dictionary of words, where the word is the key and the value is the definition of that word. Would sorting and organizing the keys in alphabetical order in the JSON file make a difference to the performance when searching a word to find its definition (plus maybe other details about that word) in JS - that is if there were thousands of words or even more?

Or does JSON and Javascript already have an algorithm built in to find results optimally without the need to sort the data for better performance?

Also, I wouldn't mind having an alternative data structure or format or library that could give faster results for this kind of search problem suggested to me! (but of course, this suggestion isn't part of the main question)

标签: javascriptjsonperformance

解决方案


对键进行排序不会影响搜索性能。Javascript 对象可能不记得插入顺序
如果可能,请使用 Map 对象 - Map 对象保存键值对并记住键的原始插入顺序。任何值(对象和原始值)都可以用作键或值
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map


推荐阅读