Comments (6)
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
isfalse
? Thecontains
method will returntrue
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.
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.
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.
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 alwaysstring
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.
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.
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)
- Crash reports after switching to use AppGroups due to 0xdead10cc HOT 2
- Cannot run in react-native 0.73.x HOT 1
- use expo go ,start with this error: CommandError: No development build (com.anonymous.***) for this project is installed. Please make and install a development build on the device first. HOT 3
- Unable to resolve module ./createMMKV.mock HOT 1
- Apple Privacy Manifest HOT 6
- Unable to make react-native-mmkv work with firebase auth persistence HOT 2
- duplicate symbols when used with react-native-quick-crypto HOT 1
- Storage breaks when calling storage.clearStore() on empty storage HOT 1
- `getString` fails on reload after calling `delete` with `encyptionKey` set in config HOT 11
- Crashing on bundle reload in beta version HOT 1
- Bug on RN 0.74 HOT 10
- How to Implement iCloud Sync to Keep Reinstalled Apps to Recover DataοΌ HOT 3
- Building for 'iOS-simulator', but linking in dylib (/opt/local/lib/libz.1.3.1.dylib) built for 'macOS' HOT 2
- After defining the path for the MMKV instance, an empty 'MMKV' directory is still generated under $(Documents) HOT 3
- Expo managment. HOT 3
- Storage data not updating to next scene HOT 2
- Beta 5 Android build error cannot find symbol HOT 9
- > Task :app:mergeDebugNativeLibs FAILED
- trim not present HOT 3
- Android armeabi-v7a support dropped by Tencent HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
π Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google β€οΈ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-mmkv.