When to use sync vs async:
- Sync methods: App initialization, reading config on startup, settings screens
- Async methods: When coordinating with other async operations, or when you prefer Promise-based code
setItemSync()
Store a string value in storage synchronously.Parameters
The storage key to set
The string value to store
Returns
Returns immediately after storing the value
Example
getItemSync()
Retrieve a string value from storage synchronously.Parameters
The storage key to retrieve
Returns
The stored string value, or
null if the key doesn’t existExample
removeItemSync()
Remove a single item from storage synchronously.Parameters
The storage key to remove
Returns
Returns immediately after removing the item
Example
clearSync()
Remove all items from storage synchronously.Returns
Returns immediately after clearing all items
Example
setObjectSync()
Store an object in storage synchronously by serializing it to JSON.Parameters
The storage key to set
The object to store (will be serialized to JSON)
Returns
Returns immediately after storing the object
Example
Objects are serialized using
JSON.stringify(), so they must be JSON-serializable.getObjectSync()
Retrieve an object from storage synchronously by deserializing from JSON.Type Parameters
The type of the object to retrieve. Defaults to a generic object type.
Parameters
The storage key to retrieve
Returns
The deserialized object, or
null if:- The key doesn’t exist
- The stored value is not valid JSON
Example
Error Handling
If the stored value cannot be parsed as JSON,getObjectSync() returns null instead of throwing an error:
Performance Notes
Sync vs Async Performance:
- Android Phone: 1000 sync ops (98ms) vs 1000 async ops (472ms) = 4.8x faster
- iOS Phone: 1000 sync ops (1098ms) vs 1000 async ops (1499ms) = 1.4x faster
- Eliminate Promise overhead
- Use direct native API calls
- Avoid bridge serialization delays
iOS Considerations
iOS sync methods are limited by UserDefaults disk I/O (~1ms per write). For bulk operations (1000+ writes), consider:- Batching operations
- Using async methods with
multiSet() - Using specialized libraries like react-native-mmkv for extreme performance needs