Code Monkey home page Code Monkey logo

Comments (6)

flixyudh avatar flixyudh commented on June 10, 2024 1

it was a good insight for me because I was not really aware about the byte sizes before.

This is also not true, lol. What if is-mmkv-2fast is false? The contains method will return true then πŸ˜„

maybe because I've playing AsyncStorage too long that I keep use set and delete method for boolean purpose πŸ€”

since it's all about the byte sizes concern, I can't complain anymore. 🀣

from react-native-mmkv.

mrousavy avatar mrousavy commented on June 10, 2024

Set

When passing a number to set, I can see that it is a number, so the type number is implied.

Get

When reading something from the storage (which is just bytes/binary), I don't know how the type is stored. There is no type information as you could overwrite it with any type, and it is also more lightweight/performant if it does not have any type information stored. So in this case you need to specify how you interpret the type to be read.

from react-native-mmkv.

flixyudh avatar flixyudh commented on June 10, 2024

When reading something from the storage (which is just bytes/binary), I don't know how the type is stored.

if so, why don't let store all value as string? since there's only 3 types (string, number, boolean), I think it's more lightweight/performant because the type always string (it would be different if react-native-mmkv also can store Map and Array).

an example of the simplification I was thinking of:

storage.set('user.name', 'Marc') 
// storage.get('user.name') 

storage.set('user.age', '21')
// storage.get('user.age')
// if dev **really** need to convert it as number, use `Number(storage.get('user.age'))` instead.

storage.set('is-mmkv-2fast', 'true')
// for boolean, i think `storage.contains('is-mmkv-2fast')` is enough, don't need for redundant method

would you mind considerating it @mrousavy ?

from react-native-mmkv.

mrousavy avatar mrousavy commented on June 10, 2024

if so, why don't let store all value as string? since there's only 3 types (string, number, boolean), I think it's more lightweight/performant because the type always string

No, why would it be more lightweight or performant to store a number or a boolean as a string?

1. Number

A number in JavaScript is represented as a 64-bit float (aka "double"), meaning it takes up 8 bytes.

The number 1 takes up 8 bytes, the number 812735 does take up 8 bytes, the number 541.5357235 takes up 8 bytes, even the number 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368 is 8 bytes.

Strings in JavaScript are usually 2 bytes or 5 bytes (depending if they are unicode or not), so if you were to represent this as a string, the string "1" would be 2 bytes, the string "812735" would be 12 bytes, the string "541.5357235" would be 22 bytes, and the string "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368" would be 618 bytes.

It goes without saying that this would be a huge waste of space.

2. Boolean

A boolean in JavaScript is represented with 1 byte. The string "true" would be 8 bytes, and "false" would be 10 bytes. This is another waste of space.

3. Efficiency

Reading a single boolean (00000001 in binary) is much more efficient than reading the binary values of the string, then decoding it into a UTF-8/UTF-16 character sequence.

Same goes for numbers.

4. Practicality

If you store a number or a boolean, you want to store a number or a boolean. Not a string. Example:

const isDarkMode = storage.getBoolean('is.dark')
const color = isDarkMode ? 'black' : 'white'

Now if we had strings:

const isDarkModeString = storage.getBoolean('is.dark')
const isDarkMode = isDarkModeString == 'true'
const color = isDarkMode ? 'black' : 'white'

This would be really annoying as you always need a separate cast. Or what if it is True instead of true? Or 1 instead of true?


With all of that said, I think it's pretty obvious that storing every value as a string is not a good idea.

from react-native-mmkv.

mrousavy avatar mrousavy commented on June 10, 2024

for boolean, i think storage.contains('is-mmkv-2fast') is enough, don't need for redundant method

This is also not true, lol. What if is-mmkv-2fast is false? The contains method will return true then πŸ˜„

from react-native-mmkv.

mrousavy avatar mrousavy commented on June 10, 2024

if dev really need to convert it as number, use Number(storage.get('user.age')) instead.

What else do you want to do with user.age? Age is a number, not a string - in 100% of the cases when you store a number you also want to read it as a number, not a string.

from react-native-mmkv.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.