So we had a case where we would have an object, where key is id (int) and the value is the string. But we noticed that most of the times, we look up id based on the string, so we decided to reverse it and make a string the key and the value is the id. Because that way instead of going through each item and compare values, we could just do var id = storage[text];
. Below are the examples of what we did.
Here's the example of the old implementation:
var storage = {
0 : null,
1 : "Hello",
2 : "world!",
3 : "How are you?"
}
Here's the example of the new implementation:
var storage = {
"null" : 0,
"Hello" : 1,
"world!" : 2,
"How are you?" : 3
}
I understand that now the string is the key and it's ok to get the same id for the same strings. But since now the string can be potentially pretty huge (slim chance, but probably max 1KB per string), is there a length limit JS or Android webview puts on the object keys?
And also, does this implementation have disadvantages? I haven't noticed any issues so far, but you never know.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…