exists(key) function #59

Closed
joshefin opened this Issue Dec 19, 2012 · 7 comments

Comments

Projects
None yet
4 participants

It would be useful to have a store.exists("somekey") function that returns true/false. Getter function can be used but it includes parsing.

yes

Owner

marcuswestin commented Dec 19, 2012

Hi guys!

I think I would be a bit surprised if JSON parsing was a bottle neck in any real world usage of store.js. Do you do have such a problem? If you do I can definitely see the value of a store.exists(key). However, if you don't I believe it may be in the interest of the project to avoid adding features without real world needs. Basically in the interest of keeping it lean, avoiding feature creep, and so on.

What do you guys think?

Cheers,
Marcus

It's not a bottleneck in most cases, but when the data is large (few thousand items) it would be useful to avoid unneeded parsing.

Owner

marcuswestin commented Dec 25, 2012

Hmm, are you sure? Like, have you tested that? I would not take it for granted that the CPU bound parsing takes significantly longer than reading the string from disk (if that's how local storage persistence works, I don't know).

You may have a point, but if you haven't collected the data then can you know that there is a need

Cheers!

Owner

marcuswestin commented Dec 25, 2012

Here's an attempt at explaining why I'm hesitant adding things without understanding why it's necessary:

First the downsides: every new feature adds complexity, bytes to download (if store.js is used on thousands of sites and downloaded by millions of people every month, then do those bytes become meaningful? Probably. We recently removed support for Firefox 2 and 3 for this reason). It also is another thing to maintain, document, and read up on when you're considering to use the project and are trying to understand it.

However, there may be upsides! And if there are, I'm all for new beneficial features! But we should really know that they are beneficial!

Let me know if you are up to get some data supporting the need, would love to know the performance of read vs parse and if there is a significant bottleneck in either or if they're about the same. If not, I'm going to vote for closing this issue. Oui?

Cheers!
Marcus

A973C commented Feb 11, 2013

👍

Hi!
I'm actually facing the same need! I use LocalStorage to store user requested information and I don't want to save a duplicated content. So if an option could exist, I also would be interested!

Based on the ReadMe example:
store.set('tags', ['javascript', 'localStorage', 'store.js'])

How can I check if user input (for example "store.js") exist in store.tags?

Cheers and thank you for your great plugin!

================================================

EDIT as closed topic:

Marcus, you've saved my day!
Thank you so much ;-)

Cheers!

Owner

marcuswestin commented Feb 11, 2013

Not quite the same issue, but here is what you'd do. Three options I guess:

Just store the tags:

store.set('javascript',true)
store.get('javascript)

Store all tags in an array:

var tags = store.get('tags') || []
var tagPresent = tags.indexOf('javascript') != -1
tags.push('javascript')
store.set('tags', tags)

Store tags in an object

var tags = store.get('tags') || {}
var tagPresent = tags['javascript']
tags['javascript'] = true
store.set('tags', tags)

Cheers!
Marcus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment