javascript - 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)
解决方案
对键进行排序不会影响搜索性能。Javascript 对象可能不记得插入顺序
如果可能,请使用 Map 对象 - Map 对象保存键值对并记住键的原始插入顺序。任何值(对象和原始值)都可以用作键或值
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map
推荐阅读
- spring-boot - 管理应用程序未启动
- c++ - C++ 分段错误问题
- python - 如何使用 pysolr 检索子值
- java - 如何结合 Scene2d 和 Bullet?
- php - PHP GET ID 和从模式中选择的复选框以插入/更新到数据库
- windows - 如何在 Windows 命令提示符下对变量进行字符串替换?
- python - Python Selenium:无法通过 Xpath 定位元素
- python - 通过 python-qrcode 生成具有恒定大小的 SVG
- scala - 具有合成 val 的 scala 案例类
- html - 如何使用 shell 脚本在两个特定的 html 标签之间插入一段代码?