A contacts library for React Native using JSI

Overview

react-native-jsi-contacts

The current react-native-contacts library uses the React Native Bridge to convert the native Java/Objective-C types to JavaScript values. This is asynchronous, batched, and serializes the huge contacts list in the native world (write into WritableArray/WritableMap, then let the Bridge convert to JSON), then deserializes it on the JavaScript side using JSON. It is therefore slow.

react-native-jsi-contacts uses JSI to be way faster.

  • Direct invocation (no batching!)
  • No JSON serialization happening
  • Directly convert object into JSI Types
  • Lazily get individual Contact fields (jsi::HostObject lazy-get)

⚠️ react-native-jsi-contacts only works on Android. If you want me to implement iOS support, consider funding the project.

Performance

The library uses almost the same native "getContacts()" function as react-native-contacts (minor tweaks to not use the Bridge types WritableArray/WritableMap), so the only difference is the conversion speed.

For 25 contacts, I have measured an average speed increase of ~35%, this greatly scales with the amount of contacts you have though.

 LOG  JSI: Contacts Permission: granted
 LOG  JSI: Got: 25 contacts in 55.14947900176048ms.
 LOG  Bridge: Contacts Permission: granted
 LOG  Bridge: Got: 25 contacts in 74.15260401368141ms.

For 25 contacts, the conversion between the native Java Contacts list and the JavaScript Contacts list takes only ~3 milliseconds!

Installation

  1. Install using npm/yarn

    npm install react-native-jsi-contacts
  2. Add this code:

    JsiContactsModule.install(reactApplicationContext);

    to your JSIModulePackage's getJSIModules method. See the react-native-mmkv installation guide on how to create a JSIModulePackage.

Sponsors

This project is sponsored by Galaxycard.

Usage

Get a list of all contacts:

import { getContactsAsync } from "react-native-jsi-contacts";

const contacts = await getContactsAsync();

Get a hashsum to compare for any changes in the contact book:

import { getHashAsync } from "react-native-jsi-contacts";
import { MMKV } from "react-native-mmkv";

const storage = new MMKV();

const hash = await getHashAsync();
const previousHash = storage.getString("contactsHash")
if (previousHash !== hash) {
  // get all contacts and reload hash now.
}

Contributing

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

License

MIT

Thanks

  • Thanks to GalaxyCard for sponsoring this project
  • Thanks to react-native-contacts for the native "getContacts()" implementation
You might also like...
Projeto pessoal: Obter a temperatura ambiente e através de um termistor ligado a um arduino e disponibilizar esses dados em tempo real via API NodeJS. No front-end os dados são acessados por uma interface em React JS.
Projeto pessoal: Obter a temperatura ambiente e através de um termistor ligado a um arduino e disponibilizar esses dados em tempo real via API NodeJS. No front-end os dados são acessados por uma interface em React JS.

INTEGRAÇÃO DA API COM OS DADOS DO ARDUINO FORNECIDOS PELO TERMISTOR Código Desenvolvido por Lucas Muffato. MATERIAIS 1 Placa de Arduino; 1 Cabo de con

A simple library that helps Android developers to execute JavaScript code from Android native side easily without using Webview.

AndroidJSModule A simple library that helps Android developers to execute JavaScript code from Android native side easily without using Webview. Insta

Draw a triangle inside Electron's window using DirectX 11, mixing web and native content.
Draw a triangle inside Electron's window using DirectX 11, mixing web and native content.

Draw a triangle inside Electron's window using DirectX 11, mixing web and native content. Limitations The native content is rendered over a child wind

A native JNI~Jme template using Ccoffee.
A native JNI~Jme template using Ccoffee.

NativeJmeTemplate Git Commands : ┌─[twisted@parrot]─[~/GradleProjects/NativeJmeTemplate] └──╼ $git init Initialized empty Git repository in /home/twis

A Native Binary Emulator Library and Utility

binmulator A Native Binary Emulator Library and Utility Most emulators for malware detection are written in Python and fail when certain emulated API

It's a static library that's provide a way to do hooking (intercepting software components) in native shared object from some Android Packages
It's a static library that's provide a way to do hooking (intercepting software components) in native shared object from some Android Packages

ARM_hook It's a static library that's provide a way to do hooking (intercepting software components) in native shared object from some Android Package

A Dart package to send native 💬 toasts on Windows. Based on WinToast.
A Dart package to send native 💬 toasts on Windows. Based on WinToast.

desktoasts A Dart package to send native 💬 toasts on Windows. Installation For Flutter dependencies: ... desktoasts: ^0.0.2 For Dart CLI here Sup

Haxe native extension to read and write windows clipboard.

Haxe Clipboard This is a native library to read and write clipboard data from Haxe. It uses Ammer to generate bindings. Note: This is a Windows only l

Jimp-native is a fast C++ re-implementation of Jimp with zero system dependencies and minimal overhead!

Jimp native Make your sever-side Jimp code run 10x faster! Jimp-native is a fast C++ re-implementation of Jimp with zero system dependencies and minim

Comments
  • reference to 'JArrayList' is ambiguous

    reference to 'JArrayList' is ambiguous

    The Android build failed with the following error:

    5 errors generated. [6/8] Building CXX object CMakeFiles/jsicontacts.dir/src/main/cpp/JSIJNIConversion.cpp.o FAILED: CMakeFiles/jsicontacts.dir/src/main/cpp/JSIJNIConversion.cpp.o E:\AndroidSDK\ndk\23.1.7779620\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=armv7-none-linux-androideabi21 --gcc-toolchain=E:/AndroidSDK/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=E:/AndroidSDK/ndk/23.1.7779620/toolchains/llvm/prebuilt/windows-x86_64/sysroot -Djsicontacts_EXPORTS -I../../../../build/fbjni-0.3.0-headers.jar -I../../../../build/third-party-ndk/boost -I../../../../build/third-party-ndk/double-conversion -I../../../../build/third-party-ndk/folly -I../../../../build/third-party-ndk/glog -IC:/TSCOProjects/Client-ReactNative/android/../node_modules/react-native/React -IC:/TSCOProjects/Client-ReactNative/android/../node_modules/react-native/React/Base -IC:/TSCOProjects/Client-ReactNative/android/../node_modules/react-native/ReactAndroid/src/main/jni -IC:/TSCOProjects/Client-ReactNative/android/../node_modules/react-native/ReactAndroid/src/main/java/com/facebook/react/turbomodule/core/jni -IC:/TSCOProjects/Client-ReactNative/android/../node_modules/react-native/ReactCommon -IC:/TSCOProjects/Client-ReactNative/android/../node_modules/react-native/ReactCommon/callinvoker -IC:/TSCOProjects/Client-ReactNative/android/../node_modules/react-native/ReactCommon/jsi -DFOLLY_NO_CONFIG=1 -DFOLLY_HAVE_CLOCK_GETTIME=1 -DFOLLY_HAVE_MEMRCHR=1 -DFOLLY_USE_LIBCPP=1 -DFOLLY_MOBILE=1 -DON_ANDROID -DONANDROID -fno-limit-debug-info -fPIC -std=c++1z -MD -MT CMakeFiles/jsicontacts.dir/src/main/cpp/JSIJNIConversion.cpp.o -MF CMakeFiles\jsicontacts.dir\src\main\cpp\JSIJNIConversion.cpp.o.d -o CMakeFiles/jsicontacts.dir/src/main/cpp/JSIJNIConversion.cpp.o -c ../../../../src/main/cpp/JSIJNIConversion.cpp In file included from ../../../../src/main/cpp/JSIJNIConversion.cpp:24: ../../../../src/main/cpp/java-bindings/JHashMap.h:17:8: error: redefinition of 'JHashMap' struct JHashMap : JavaClass<JHashMap<K, V>, JMap<K, V>> { ^ ../../../../build/fbjni-0.3.0-headers.jar\fbjni/detail/Iterator.h:170:8: note: previous definition is here struct JHashMap : JavaClass<JHashMap<K, V>, JMap<K, V>> { ^ In file included from ../../../../src/main/cpp/JSIJNIConversion.cpp:25: ../../../../src/main/cpp/java-bindings/JContact.h:50:15: error: reference to 'JArrayList' is ambiguous local_ref<JArrayList<JItem::javaobject>> getUrls(); ^ ../../../../build/fbjni-0.3.0-headers.jar\fbjni/detail/Iterator.h:162:8: note: candidate found by name lookup is 'facebook::jni::JArrayList' struct JArrayList : JavaClass<JArrayList<E>, JList<E>> { ^ ../../../../src/main/cpp/java-bindings/JArrayList.h:18:8: note: candidate found by name lookup is 'vision::JArrayList' struct JArrayList : JavaClass<JArrayList<E>, JList<E>> { ^ In file included from ../../../../src/main/cpp/JSIJNIConversion.cpp:25: ../../../../src/main/cpp/java-bindings/JContact.h:51:15: error: reference to 'JArrayList' is ambiguous local_ref<JArrayList<JItem::javaobject>> getInstantMessengers(); ^ ../../../../build/fbjni-0.3.0-headers.jar\fbjni/detail/Iterator.h:162:8: note: candidate found by name lookup is 'facebook::jni::JArrayList' struct JArrayList : JavaClass<JArrayList<E>, JList<E>> { ^ ../../../../src/main/cpp/java-bindings/JArrayList.h:18:8: note: candidate found by name lookup is 'vision::JArrayList' struct JArrayList : JavaClass<JArrayList<E>, JList<E>> { ^ In file included from ../../../../src/main/cpp/JSIJNIConversion.cpp:25: ../../../../src/main/cpp/java-bindings/JContact.h:54:15: error: reference to 'JArrayList' is ambiguous local_ref<JArrayList<JItem::javaobject>> getEmails(); ^ ../../../../build/fbjni-0.3.0-headers.jar\fbjni/detail/Iterator.h:162:8: note: candidate found by name lookup is 'facebook::jni::JArrayList' struct JArrayList : JavaClass<JArrayList<E>, JList<E>> { ^ ../../../../src/main/cpp/java-bindings/JArrayList.h:18:8: note: candidate found by name lookup is 'vision::JArrayList' struct JArrayList : JavaClass<JArrayList<E>, JList<E>> { ^ In file included from ../../../../src/main/cpp/JSIJNIConversion.cpp:25: ../../../../src/main/cpp/java-bindings/JContact.h:55:15: error: reference to 'JArrayList' is ambiguous local_ref<JArrayList<JItem::javaobject>> getPhones(); ^ ../../../../build/fbjni-0.3.0-headers.jar\fbjni/detail/Iterator.h:162:8: note: candidate found by name lookup is 'facebook::jni::JArrayList' struct JArrayList : JavaClass<JArrayList<E>, JList<E>> { ^ ../../../../src/main/cpp/java-bindings/JArrayList.h:18:8: note: candidate found by name lookup is 'vision::JArrayList' struct JArrayList : JavaClass<JArrayList<E>, JList<E>> { ^ In file included from ../../../../src/main/cpp/JSIJNIConversion.cpp:25: ../../../../src/main/cpp/java-bindings/JContact.h:56:15: error: reference to 'JArrayList' is ambiguous local_ref<JArrayList<JPostalAddressItem::javaobject>> getPostalAddresses(); ^ ../../../../build/fbjni-0.3.0-headers.jar\fbjni/detail/Iterator.h:162:8: note: candidate found by name lookup is 'facebook::jni::JArrayList' struct JArrayList : JavaClass<JArrayList<E>, JList<E>> { ^ ../../../../src/main/cpp/java-bindings/JArrayList.h:18:8: note: candidate found by name lookup is 'vision::JArrayList' struct JArrayList : JavaClass<JArrayList<E>, JList<E>> { ^ 6 errors generated. ninja: build stopped: subcommand failed.

    opened by omidshafai 0
  • App crashing on getContactsAsync

    App crashing on getContactsAsync

    Hello, thanks for making this library and converting the library into JSI, I've installed the library and after running the example code my app crash immediately. Here is the code below I'm trying with:

      useEffect(() => {
        (async () => {
          const contacts = await getContactsAsync();
          console.log('COntacts => ', contacts);
        })();
      }, []);
    
    opened by anwersolangi 3
Releases(v0.2.3)
Owner
Marc Rousavy
they call me ranch cause I be dressing
Marc Rousavy
A React Native library for accessing an ArrayBuffer of a Blob instance.

react-native-blob-jsi-helper A React Native library for directly accessing an ArrayBuffer of a Blob instance. Note: This is a workaround. A long-term

Marc Rousavy 82 Nov 17, 2022
A fast base64 module for React Native

react-native-quick-base64 A native implementation of Base64 in C++ for React Native. 4x faster than base64-js on an iPhone 11 Pro.

Takuya Matsuyama 228 Dec 18, 2022
A framework for building native Windows apps with React.

React Native for Windows Build native Windows apps with React. See the official React Native website for an introduction to React Native. React Native

Microsoft 15.2k Jan 2, 2023
React-Native build of Hyperswarm and dependencies

Testing Hyperbeam Start hyperbeam on your computer and copy-paste address to packages/mobile/App.tsx. Then in another terminal run yarn mobile:start a

Tomas Ravinskas 5 Sep 5, 2022
React Native polyfill for crypto.getRandomValues. Used in libraries like uuid.

react-native-random-values-jsi-helper React Native polyfill for crypto.getRandomValues. Used in libraries like uuid. Installation yarn add react-nativ

Matei Oprea 30 Oct 30, 2022
A step by step example of creating your own React Native TurboModule.

Discovering Turbo Modules Note: This example was made in RN 0.63. Some things have changed since then, but the good news is that now the setup should

Bartłomiej Klocek 96 Dec 5, 2022
🖼️ A writeable in-memory Image JSI Host Object

??️ react-native-jsi-image ??️ This library is work in progress! ??️ A writeable in-memory Image JSI Host Object. JSI-Image is a modern library that p

Marc Rousavy 261 Dec 20, 2022
Visual Studio native debugger extension to help debug native applications using Mono.

Unity Mixed Callstack UnityMixedCallstack is a Visual Studio 2017/2019 extension to help debug native applications embedding Mono, like Unity. If you

Unity Technologies 83 Nov 28, 2022
C++React: A reactive programming library for C++11.

C++React is reactive programming library for C++14. It enables the declarative definition of data dependencies between state and event flows. Based on

Sebastian 969 Jan 3, 2023
Plays native alert sound and shows native dialogs/alerts in your Flutter app.

flutter_platform_alert 2021 © Weizhong Yang a.k.a zonble. A simple plugin to present native alerts, including playing alert sounds and showing alert d

Weizhong Yang a.k.a zonble 60 Dec 21, 2022