Giter VIP home page Giter VIP logo

wildanfuady / react-native-mmkv Goto Github PK

View Code? Open in Web Editor NEW

This project forked from mrousavy/react-native-mmkv

0.0 1.0 0.0 1.36 MB

⚡️ An extremely fast key/value storage library for React Native. ~30x faster than AsyncStorage!

License: MIT License

CMake 2.48% C++ 16.15% Kotlin 2.82% JavaScript 3.26% Java 14.34% TypeScript 12.19% Swift 0.13% C 0.24% Objective-C 13.91% Ruby 2.89% Objective-C++ 31.60%

react-native-mmkv's Introduction

MMKV


MMKV is an efficient, small mobile key-value storage framework developed by WeChat.

See Tencent/MMKV for more information

react-native-mmkv is a library that allows you to use MMKV inside your React Native applications.

Features

  • Get and set strings, booleans and numbers
  • Fully synchronous calls, no async/await, no Promises, no Bridge.
  • High performance because everything is written in C++ (even the JS functions have C++ bodies!)
  • ~30x faster than AsyncStorage
  • Uses JSI instead of the "old" Bridge

Fun fact: since all the JS functions have C++ implementations, you can also directly call them in reanimated worklets

Benchmark

AsyncStorage vs MMKV: Reading a value from Storage 1000 times.
Measured in milliseconds on an iPhone 8, lower is better.

Installation

npm install react-native-mmkv
cd ios && pod install

Usage

Set

import { MMKV } from 'react-native-mmkv';

MMKV.set('Marc', 'user.name')
MMKV.set(20, 'user.age')
MMKV.set(true, 'is-mmkv-fast-asf')

Get

import { MMKV } from 'react-native-mmkv';

const username = MMKV.getString('user.name') // 'Marc'
const age = MMKV.getNumber('user.age') // 20
const isMmkvFastAsf = MMKV.getBoolean('is-mmkv-fast-asf') // true

Delete

import { MMKV } from 'react-native-mmkv';

MMKV.delete('user.name')

Get all keys

import { MMKV } from 'react-native-mmkv';

const keys = MMKV.getAllKeys() // ['user.name', 'user.age', 'is-mmkv-fast-asf']

Objects

import { MMKV } from 'react-native-mmkv';

const user = {
  username: 'Marc',
  age: 20
}

MMKV.set(JSON.stringify(user), 'user')

const jsonUser = MMKV.getString('user') // { 'username': 'Marc', 'age': 20 }
const userObject = JSON.parse(jsonUser)

redux-persist

If you want to use MMKV with redux-persist, create the following storage object:

import { MMKV } from "react-native-mmkv";
import { Storage } from "redux-persist";

type StorageType = typeof MMKV & {
  /**
   * Redux Persist plugin for react-native-mmkv
   */
  redux: Storage;
};

// Unfortunately redux-persist expects Promises, so we have to wrap our sync calls with Promise resolvers/rejecters
const storage: StorageType = {
  redux: {
    setItem: (key: string, value: string): Promise<boolean> => {
      MMKV.set(value, key);
      return Promise.resolve(true);
    },
    getItem: (key: string): Promise<string> =>
      Promise.resolve(MMKV.getString(key)),
    removeItem: (key: string): Promise<void> => {
      MMKV.delete(key);
      return Promise.resolve();
    },
  },
  ...MMKV,
};

export default storage;

Contributing

See the contributing guide to learn how to contribute to the repository and the development workflow.

License

MIT

react-native-mmkv's People

Contributors

mrousavy avatar

Watchers

James Cloos avatar

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.