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...
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.
I expect that writing 50 elements into Realm to be faster than writing into AsyncStorage. Preferably to take no more than 1-50 ms.


/* 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;
there is some known performance issues while in the debug mode , refer to https://github.com/realm/realm-js/issues/491 & https://github.com/realm/realm-js#issues-with-debugging for more details. @MD3XTER did you check the same in the production mode? does that have the same performance problem?
for benchmarking multiple writes, have a look at the sample given by @bmunkholm at https://github.com/realm/realm-js/issues/2154#issuecomment-453068402 ,
const Realm = require('realm')
Realm.open({
schema: [{
name: 'Page',
properties: {
id: 'int'
}
}]
}).then(realm => {
console.time('nothing')
console.timeEnd('nothing')
console.time('warmup')
realm.write(() => {
for (let i = 0; i < 10; i++) {
realm.create('Page', {id: 5})
}
})
console.timeEnd('warmup')
for (let objects=0; objects<=1000; objects+=100) {
let test = 'Objects ' + objects
console.time(test)
realm.write(() => {
for (let i = 0; i < 100; i++) {
realm.create('Page', {id: 5})
}
});
console.timeEnd(test)
}
process.exit()
});
@vazra thank you for your quick reply. I am not using a debugger. I use Reactotron which could make realm-js slow. I have tested the app in production mode. How can I do this benchmark test in production mode? The code that you have attached above is using console.time function. How can I see this in production?
@MD3XTER try logging to console w/o Reactotron, and check if it's different. By production I mean, not in the development mode, I don't think there's something blocking you from printing to console in non-dev mode. can you clarify?
@vazra Same issue. On app install, I load a significant amount of data from a static json file into the realm database. Using version 4.0.0-beta.0 this data loads in a few seconds, using 6.0.0 the same load can take up to 10 minutes. Tests are run in production.
Within a single write, I loop through the json file and load the database.
@jimsideout It shouldn't be that slow, what kind of data are you inserting? can you update with a reproducible sample. I ran the above code in realm 5 and 6 , the results are
With Realm 5.0.5 (with Node 12 , MacBook Pro)
nothing: 0.107ms
warmup: 1.972ms
Objects 0: 1.884ms
Objects 100: 1.636ms
Objects 200: 1.783ms
Objects 300: 1.879ms
Objects 400: 1.543ms
Objects 500: 1.621ms
Objects 600: 1.634ms
Objects 700: 1.648ms
Objects 800: 1.792ms
Objects 900: 1.773ms
Objects 1000: 1.611ms
With Realm 6.0.2 (with Node 12 , MacBook Pro)
nothing: 0.107ms
warmup: 1.992ms
Objects 0: 1.588ms
Objects 100: 1.511ms
Objects 200: 1.554ms
Objects 300: 1.767ms
Objects 400: 1.554ms
Objects 500: 1.467ms
Objects 600: 1.457ms
Objects 700: 1.495ms
Objects 800: 1.706ms
Objects 900: 1.498ms
Objects 1000: 1.421ms
@jimsideout @MD3XTER In case, if you are working with electron, have a look at the Realm part of Electron React NativeDBs demo available at https://github.com/vazra/electron-react-ts-rxdb-realm-sqlite , in which you can add as much as dummy data and check the performance.
@vazra looks like the same issue as https://github.com/realm/realm-js/issues/2845
The problem is when creating an object with nested objects. My data is just JSON data that is being looped through. Nothing special, just objects containing about 40 values of strings, ints, bools, and 2 references to other objects.
Ok. Looks like your issue is indeed what's listed in #2845. @MD3XTER can you confirm that assumption?
@bmunkholm yes, I would say so. Let's close this issue and track #2845 instead. Thank you all for your help, hopefully, we can find a solution for this!
Most helpful comment
@bmunkholm yes, I would say so. Let's close this issue and track #2845 instead. Thank you all for your help, hopefully, we can find a solution for this!