在JavaScript或许多较小的对象中使用一个大对象会更好吗? [英] Is it better to have one large object in JavaScript or many smaller ones?

查看:50
本文介绍了在JavaScript或许多较小的对象中使用一个大对象会更好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个JS库,可以阅读国际象棋游戏,将它们变成可重玩的游戏,在一个网页中可以有很多游戏(一个在自己的div中),我想知道的是 - 考虑性能 - 如果最好有一个大对象保存所有游戏的所有动作或许多较小的对象,每个对象存储一个游戏的动作。

I'm writing a JS lib that reads chess games to turn them into re playable games, in one single web page there can be many games (one in its own div), what I'm wondering is – thinking about performance – if it's better to have one large object that holds all the moves of all the games or many smaller objects each storing the moves of one game.

我意识到这是也许是整个优化过程中的一个小点,但它是我现在要解决的问题。

I realize that this is perhaps a small point in the whole optimization process but its the one that I want to address now.

推荐答案

Donald Knuth:我们应该忘记小的效率,比如大约97%的时间:过早的优化是所有邪恶的根源

Donald Knuth: "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil"

首先为你的游戏设计一个正确的数据模型从域建模的角度来看是自然的。

Start by designing a data model for your game that is right and natural from a domain modelling point of view.

构建软件。

然后,当你是在开发阶段,分析性能是有意义的,制定一些低性能环境哟你希望你的游戏可以在你的游戏中进行测试。

Then, when you're at a stage in development that analysing performance makes sense, work out some low performance environments you'd like your game to work in, and test in them.

你可能会发现你没有任何问题,正如其他人在回答这个问题时所说的那样。

You'll probably find you have no issues, as others have stated in answers to this question.

如果您发现问题,请分析您的代码并找出原因。优化您需要的代码。即使您发现了性能问题,只要您设计得很好,它就不太可能是由您的数据模型引起的。

If you find an issue, profile your code and discover the cause. Optimise the code you need to. Even if you find a performance issue, it's unlikely to be caused by your data model, as long as you've designed it well.

如果数据模型确实是一个性能问题,那么只有在您需要达到所需性能的前提下,才会损害您的初始设计,并记录你做出的妥协,以及为什么不得不这样做。

If data model really is a performance issue, only then should you compromise your initial design only to the extent you need to in order to get the performance you need, documentating the compromises you've made, and why you had to.

这篇关于在JavaScript或许多较小的对象中使用一个大对象会更好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆