react-native-mmkv-storage
An ultra fast (0.0002s read/write), small & encrypted mobile key-value storage framework for React Native written in C++ using JSI
Top Related Projects
Realm is a mobile database: an alternative to SQLite & key-value stores
An asynchronous, persistent, key-value storage system for React Native.
⚡️ The fastest key/value storage for React Native. ~30x faster than AsyncStorage!
Full featured SQLite3 Native Plugin for React Native (Android and iOS)
Quick Overview
React Native MMKV Storage is a fast and efficient key-value storage system for React Native applications. It's built on top of MMKV, a high-performance key-value storage framework developed by WeChat. This library provides a simple API for storing and retrieving data in React Native apps with excellent performance characteristics.
Pros
- Extremely fast read and write operations compared to AsyncStorage
- Supports storing various data types including objects, arrays, and numbers
- Offers encryption for sensitive data storage
- Provides a familiar API similar to AsyncStorage for easy adoption
Cons
- Limited to key-value storage, not suitable for complex data structures
- Requires additional setup compared to built-in AsyncStorage
- May have a slightly larger app size due to native dependencies
- Limited cross-platform support (mainly iOS and Android)
Code Examples
- Basic usage:
import MMKVStorage from 'react-native-mmkv-storage';
const MMKV = new MMKVStorage.Loader().initialize();
// Set a value
await MMKV.setStringAsync('user', 'John Doe');
// Get a value
const user = await MMKV.getStringAsync('user');
console.log(user); // Output: John Doe
- Storing and retrieving objects:
const user = { name: 'John', age: 30 };
await MMKV.setMapAsync('userObject', user);
const retrievedUser = await MMKV.getMapAsync('userObject');
console.log(retrievedUser); // Output: { name: 'John', age: 30 }
- Using encryption:
const encryptedMMKV = new MMKVStorage.Loader().withEncryption().initialize();
await encryptedMMKV.setStringAsync('secretKey', 'sensitive data');
const secret = await encryptedMMKV.getStringAsync('secretKey');
console.log(secret); // Output: sensitive data
Getting Started
-
Install the package:
npm install react-native-mmkv-storage
-
For iOS, run:
cd ios && pod install
-
Import and initialize in your React Native app:
import MMKVStorage from 'react-native-mmkv-storage'; const MMKV = new MMKVStorage.Loader().initialize();
-
Start using the API to store and retrieve data:
await MMKV.setStringAsync('key', 'value'); const value = await MMKV.getStringAsync('key');
Competitor Comparisons
Realm is a mobile database: an alternative to SQLite & key-value stores
Pros of realm-js
- Offers a more comprehensive database solution with advanced querying capabilities
- Supports real-time synchronization across devices and platforms
- Provides built-in encryption for data security
Cons of realm-js
- Larger file size and potentially higher memory usage
- Steeper learning curve due to more complex API and concepts
- May be overkill for simple key-value storage needs
Code Comparison
react-native-mmkv-storage:
import MMKVStorage from 'react-native-mmkv-storage';
const MMKV = new MMKVStorage.Loader().initialize();
MMKV.setStringAsync("key", "value");
const value = await MMKV.getStringAsync("key");
realm-js:
import Realm from 'realm';
const realm = await Realm.open({schema: [MySchema]});
realm.write(() => {
realm.create('MyObject', {key: 'value'});
});
const objects = realm.objects('MyObject');
realm-js offers a more robust database solution with advanced features, while react-native-mmkv-storage provides a simpler key-value storage approach. The choice between them depends on the specific requirements of your project, such as data complexity, synchronization needs, and performance considerations.
An asynchronous, persistent, key-value storage system for React Native.
Pros of async-storage
- Widely adopted and well-maintained by the React Native community
- Simple API, easy to use for basic key-value storage needs
- Cross-platform compatibility (iOS, Android, and web)
Cons of async-storage
- Slower performance compared to MMKV-based storage
- Limited functionality for complex data structures or large datasets
- Asynchronous nature can lead to potential race conditions if not handled properly
Code Comparison
async-storage:
import AsyncStorage from '@react-native-async-storage/async-storage';
await AsyncStorage.setItem('key', 'value');
const value = await AsyncStorage.getItem('key');
react-native-mmkv-storage:
import MMKVStorage from 'react-native-mmkv-storage';
const MMKV = new MMKVStorage.Loader().initialize();
MMKV.setStringAsync('key', 'value');
const value = await MMKV.getStringAsync('key');
The code examples show that both libraries offer similar basic functionality for storing and retrieving data. However, react-native-mmkv-storage provides additional methods for different data types and offers synchronous operations, which can be beneficial in certain scenarios.
While async-storage is more widely used and has a simpler API, react-native-mmkv-storage offers better performance and more advanced features. The choice between the two depends on the specific requirements of your project, such as performance needs, data complexity, and cross-platform compatibility.
⚡️ The fastest key/value storage for React Native. ~30x faster than AsyncStorage!
Pros of react-native-mmkv
- Higher performance due to direct C++ bindings
- Smaller package size and fewer dependencies
- More active development and frequent updates
Cons of react-native-mmkv
- Less extensive API and fewer utility functions
- Lacks built-in encryption support
- May require more manual type handling
Code Comparison
react-native-mmkv-storage:
import MMKVStorage from 'react-native-mmkv-storage';
const MMKV = new MMKVStorage.Loader().initialize();
await MMKV.setStringAsync("key", "value");
const value = await MMKV.getStringAsync("key");
react-native-mmkv:
import { MMKV } from 'react-native-mmkv';
const storage = new MMKV();
storage.set("key", "value");
const value = storage.getString("key");
Both libraries provide similar core functionality for key-value storage in React Native applications. react-native-mmkv-storage offers a more comprehensive API with additional features like encryption and utility functions, making it easier to use for complex storage needs. On the other hand, react-native-mmkv focuses on performance and simplicity, providing a lightweight solution with direct C++ bindings. The choice between the two depends on the specific requirements of your project, balancing between feature richness and raw performance.
Full featured SQLite3 Native Plugin for React Native (Android and iOS)
Pros of react-native-sqlite-storage
- Supports complex SQL queries and relational data structures
- Offers robust data integrity and ACID compliance
- Widely used and well-established in the developer community
Cons of react-native-sqlite-storage
- Generally slower performance compared to MMKV
- Requires more setup and configuration
- Larger file size and memory footprint
Code Comparison
react-native-sqlite-storage:
const db = SQLite.openDatabase({ name: 'mydb.db', location: 'default' });
db.transaction((tx) => {
tx.executeSql('INSERT INTO Users (name, age) VALUES (?, ?)', ['John', 30]);
});
react-native-mmkv-storage:
import MMKVStorage from 'react-native-mmkv-storage';
const MMKV = new MMKVStorage.Loader().initialize();
MMKV.setString('user', JSON.stringify({ name: 'John', age: 30 }));
react-native-sqlite-storage is better suited for complex data structures and relationships, offering full SQL capabilities. However, it comes with a performance trade-off and requires more setup. react-native-mmkv-storage, on the other hand, provides simpler and faster key-value storage but lacks advanced querying capabilities. The choice between the two depends on the specific needs of your project, balancing between data complexity and performance requirements.
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME

Install the library
npm install react-native-mmkv-storage
For expo bare workflow
expo prebuild
What it is
This library aims to provide a fast & reliable solution for you data storage needs in react-native apps. It uses MMKV by Tencent under the hood on Android and iOS both that is used by their WeChat app(more than 1 Billion users). Unlike other storage solutions for React Native, this library lets you store any kind of data type, in any number of database instances, with or without encryption in a very fast and efficient way. Read about it on this blog post I wrote on dev.to
Learn how to build your own module with JSI on my blog
0.9.0 Breaking change
Works only with react native 0.71.0 and above. If you are on older version of react native, keep using 0.8.x.
Features
Written in C++ using JSI
Starting from v0.5.0
the library has been rewritten in C++ on Android and iOS both. It employs React Native JSI making it the fastest storage option for React Native.
Simple and lightweight
(~ 50K Android/30K iOS) and even smaller when packaged.
Fast and Efficient (0.0002s Read/Write Speed)
MMKV uses mmap to keep memory synced with file, and protobuf to encode/decode values to achieve best performance. You can see the benchmarks here: Android & iOS
Reactive using useMMKVStorage
& useIndex
Hooks
Hooks let's the storage update your app when a change takes place in storage.
useMMKVStorage
hook
Starting from v0.5.5
, thanks to the power of JSI, we now have our very own useMMKVStorage
Hook. Think of it like a persisted state that will always write every change in storage and update your app UI instantly. It doesn't matter if you reload the app or restart it.
import { MMKVLoader, useMMKVStorage } from 'react-native-mmkv-storage';
const storage = new MMKVLoader().initialize();
const App = () => {
const [user, setUser] = useMMKVStorage('user', storage, 'robert');
const [age, setAge] = useMMKVStorage('age', storage, 24);
return (
<View style={styles.header}>
<Text style={styles.headerText}>
I am {user} and I am {age} years old.
</Text>
</View>
);
};
Learn more about useMMKVStorage
hook it in the docs.
useIndex
hook
A hook that will take an array of keys and returns an array of values for those keys. This is supposed to work in combination with Transactions. When you have build your custom index, you will need an easy and quick way to load values for your index. useIndex hook actively listens to all read/write changes and updates the values accordingly.
const App = () => {
// Get list of all post ids
const postsIndex = useMMKVStorage("postsIndex",storage,[]); // ['post123','post234'];
// Get the posts based on those ids.
const [posts,update,remove] = useIndex(postsIndex,"object" storage);
return <View>
<FlatList
data={posts}
renderItem={...}
>
</View>
}
Learn more about useIndex
hook it in the docs.
Lifecycle Control with Transaction Manager
Listen to a value's lifecycle and mutate it on the go. Transactions lets you register lifecycle functions with your storage instance such as Read, Write and Delete. This allows for a better and more managed control over the storage and also let's you build custom indexes with a few lines of code.
MMKV.transactions.register('object', 'beforewrite', ({ key, value }) => {
if (key.startsWith('post.')) {
// Call this only when the key has the post prefix.
let indexForTag = MMKV.getArray(`${value.tag}-index`) || [];
MMKV.setArray(indexForTag.push(key));
}
});
Learn more about how to use Transactions in docs
Multi-Process Support
MMKV supports concurrent read-read and read-write access between processes. This means that you can use MMKV for various extensions and widgets and your app.
Create unlimited Database instances
You can create many database instances. This helps greatly if you have separate logics/modules in the same app that use data differently, It also helps in better performance since each database instance is small instead of a single bulky database which makes things slower as it grows.
const userStorage = new MMKVLoader().withEncryption().withInstanceID('userdata').initialize();
const settingsStorage = new MMKVLoader().withInstanceID('settings').initialize();
Full encryption support
The library supports full encryption (AES CFB-128) on Android and iOS. You can choose to store your encryption key securely for continuious usage. The library uses Keychain on iOS and Android Keystore on android (API 23 and above). Encrypting an instance is simple:
const storage = new MMKVLoader()
.withEncryption() // Generates a random key and stores it securely in Keychain
.initialize();
And that's it.
Simple indexer and data querying
For each database instance, there is one global key index and then there are indexes of each type of data. So querying is easy and fast.
Supports redux-persist
Support for redux persist is also added starting from v0.3.2.
Supports expo
You can use this library with expo bare workflow.
Flipper plugin
Thanks to pnthach95 Flipper plugin is finally here. https://github.com/pnthach95/flipper-plugin-react-native-mmkv-storage. It supports logging and manipulating storage values on the fly.
Consider supporting with a âï¸ star on GitHub
If you are using the library in one of your projects, consider supporting with a star. It takes a lot of time and effort to keep this maintained and address issues and bugs. Thank you.
Contact & Support
- Create a GitHub issue for bug reports, feature requests, or questions
- Follow @ammarahm-ed
I want to contribute
That is awesome news! There is a lot happening at a very fast pace in this library right now. Every little help is precious. You can contribute in many ways:
- Suggest code improvements on native iOS and Android
- If you have suggestion or idea you want to discuss, open an issue.
- Open an issue if you want to make a pull request, and tell me what you want to improve or add so we can discuss
- I am always open to new ideas
Special thanks to
- ospfranco and his work on react-native-quick-sqlite library which inspired async read/write.
License
This library is licensed under the MIT license.
Copyright © Ammar Ahmed (@ammarahm-ed)

Top Related Projects
Realm is a mobile database: an alternative to SQLite & key-value stores
An asynchronous, persistent, key-value storage system for React Native.
⚡️ The fastest key/value storage for React Native. ~30x faster than AsyncStorage!
Full featured SQLite3 Native Plugin for React Native (Android and iOS)
Convert
designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot