扁平物体的缺点?(Disadvantages to flattening object?)

编程入门 行业动态 更新时间:2024-10-27 02:18:05
扁平物体的缺点?(Disadvantages to flattening object?)

我有一些数据非常适合嵌套的分类结构。 起初我使用嵌套映射来表示它,但我想也许使用由包含每个类别关键字的元组索引的扁平结构可能更节省空间。

例如,假设我有数据狗,猫,树,金,银首先,让我们说,我有以下结构,其中箭头表示地图中的键值对

animal -> {barks -> "dog" purrs -> "cat"} plant -> {rooted -> "tree"} mineral -> {golden -> "gold" silvery -> "silver"}

但现在,我在想为什么不这样做

['animal','barks'] -> 'dog' ['animal', 'purrs'] -> 'cat' ['plant', 'rooted'] -> 'tree' ['mineral','golden'] -> 'gold ['mineral', 'silvery'] -> 'silver'

所有在同一个平面物体?

这有什么表现,风格或其他缺点吗?

我能想到的是,在这个例子中,更难获得所有矿物质或所有动物 - 我必须通过线性搜索所有关键来收集它。 还有别的事吗?

I have some data that lends itself well to a nested categorical structure. At first I was using nested maps to represent it, but I'm thinking maybe it would be more space-efficient to just use a flat structure that's indexed into by tuples containing each category keyword.

For example, let's say I have the data dog,cat,tree,gold,silver At first, let's say, I had the following structure, where an arrow denotes a key-value pair in a map

animal -> {barks -> "dog" purrs -> "cat"} plant -> {rooted -> "tree"} mineral -> {golden -> "gold" silvery -> "silver"}

But now, I'm thinking why not just have

['animal','barks'] -> 'dog' ['animal', 'purrs'] -> 'cat' ['plant', 'rooted'] -> 'tree' ['mineral','golden'] -> 'gold ['mineral', 'silvery'] -> 'silver'

all in the same one flat object?

Are there any performance, style, or otherwise disadvantages to this?

One I can think of is that it's harder to, in this example, get all minerals, or all animals - I have to do a linear search through all the keys to collect that. Is there anything else?

最满意答案

好吧,首先,直到ES6得到广泛支持,你将无法使用数组作为对象键,因此你必须包含一些其他级别的间接来正确存储事物,可能是这样的:

{ 'animal~barks': 'dog', 'animal~purrs': 'cat', ... }

这真的使编码变得更难。

你也会增加你的记忆消耗,大概是通过重复,例如'动物'来收集大量的内存。

所以我认为有几次打击这个。

Well, first off, until ES6 becomes widely supported, you won't able to use arrays as object keys, so you would have to then include some other level of indirection to store things properly, perhaps something like:

{ 'animal~barks': 'dog', 'animal~purrs': 'cat', ... }

That really makes coding with this harder.

You also would increase your memory consumption, presumably with large collections by repeating, e.g., 'animal'.

So I think there are several strikes against this.

更多推荐

本文发布于:2023-08-01 15:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1361586.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:物体   缺点   Disadvantages   flattening   object

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!