Uploaded image for project: 'Realm JavaScript SDK'
  1. Realm JavaScript SDK
  2. RJS-644

Slow performance on writing multiple objects at a time

    • Type: Icon: Task Task
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: None
    • Component/s: None

      j                                                                 Hello, I am using Realm in a React Native project and I am experiencing some performance issues. Maybe this is normal, but it's too slow for me. Or maybe my implementation is wrong...

      Goals

      I want to write 600 elements into a realm database at a very fast speed. For the sake of the test I am writing just 50 elements, but it's still very slow and writing 600 elements into realm will block JS thread for too long.

      Expected Results

      I expect that writing 50 elements into Realm to be faster than writing into AsyncStorage. Preferably to take no more than 1-50 ms.

      Actual Results

      Realm Benchmark

      AsyncStorage Benchmark

      Code Sample

      /* Realm - Implementation */
      
      const bench = Reactotron.benchmark!('Realm - addPeaks');
      
      const realm = await Realm.open(this.realmConfig);
      
      bench.step('open db');
      
      realm.write(() => {
      peaks.forEach((peak, index) => {
        realm.create('Peak', {
          index: this.blockIndex + index,
          amplitude: peak[0],
          IMU: {
            x: peak[1],
            y: peak[2],
            z: peak[3],
          },
          peakDetected: peak[4],
        });
      });
      });
      
      bench.step('write in db');
      
      this.blockIndex += peaks.length;
      
      realm.close();
      
      bench.stop('close db');
      
      /* AsyncStorage - Implementation */
      
      const bench = Reactotron.benchmark!('AsyncStorage - addPeaks');
      
      const peakObjects = peaks.map((peak, index) => ({
        index: this.blockIndex + index,
        amplitude: peak[0],
        IMU: {
          x: peak[1],
          y: peak[2],
          z: peak[3],
        },
        peakDetected: peak[4],
      }));
      
      bench.step('create objects');
      
      const jsonPeaks = JSON.stringify(peakObjects);
      
      bench.step('stringify objects');
      
      await AsyncStorage.setItem(
        `@sessionPeaks_${this.blockIndex}`,
        JSON.stringify(jsonPeaks)
      );
      
      bench.stop('write in AS');
      
      this.blockIndex += peaks.length;
      

      Version of Realm and Tooling

      • Realm JS SDK Version: 6.0.2
      • Node or React Native: React Native - 0.62.2
      • Client OS & Version: iPhone 6s - iOS 13.5.1 (development environment)
      • Which debugger for React Native: Reactotron

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: