Which is Better JavaScript Object Property Access Libraries?
dot-prop vs lodash.get vs lodash.set vs object-path
1 Year
dot-proplodash.getlodash.setobject-pathSimilar Packages:
What's JavaScript Object Property Access Libraries?

These libraries provide utility functions for accessing and manipulating properties of JavaScript objects, particularly when dealing with deeply nested structures. They simplify the process of getting and setting values in objects, which can be cumbersome and error-prone when done manually. Each library has its unique features and design philosophies, catering to different needs in object property handling. Their primary aim is to enhance code readability and maintainability by providing a consistent API for property access, especially in complex data structures.

NPM Package Downloads Trend
Github Stars Ranking
Stat Detail
Package
Downloads
Stars
Size
Issues
Publish
License
dot-prop18,394,08981216.9 kB85 months agoMIT
lodash.get12,168,60959,714-1088 years agoMIT
lodash.set1,870,34759,714-1088 years agoMIT
object-path1,813,0501,059-343 years agoMIT
Feature Comparison: dot-prop vs lodash.get vs lodash.set vs object-path

Property Access

  • dot-prop: dot-prop allows you to access and manipulate properties of an object using dot-notation strings, making it easy to retrieve values from deeply nested structures with minimal code.
  • lodash.get: lodash.get provides a safe way to access nested properties, returning undefined if the property does not exist, thus preventing runtime errors. It also supports default values if the property is not found.
  • lodash.set: lodash.set enables you to set values at a specified path in an object, creating nested objects as necessary. This is particularly useful for updating complex data structures without mutating the original object.
  • object-path: object-path offers both get and set functionalities with support for dot and bracket notation, allowing for flexible property access and manipulation. It also includes methods for deleting properties and checking for existence.

Error Handling

  • dot-prop: dot-prop does not throw errors for undefined properties, allowing for safe access without additional checks. It simplifies error handling in cases where properties may not exist.
  • lodash.get: lodash.get is designed to handle undefined properties gracefully, returning undefined instead of throwing an error, which is useful for avoiding crashes in applications.
  • lodash.set: lodash.set does not provide specific error handling for non-existent paths but ensures that the object structure is created as needed, preventing errors during property assignment.
  • object-path: object-path provides robust error handling and allows you to check for property existence before accessing or modifying it, making it safer for complex operations.

Performance

  • dot-prop: dot-prop is lightweight and optimized for performance, making it suitable for scenarios where minimal overhead is essential, especially in performance-critical applications.
  • lodash.get: lodash.get is part of the Lodash library, which is optimized for performance, but may introduce some overhead if used in isolation without other Lodash functions.
  • lodash.set: lodash.set is also optimized for performance, particularly when used in conjunction with lodash.get, ensuring efficient updates to nested properties without unnecessary computations.
  • object-path: object-path is slightly heavier than dot-prop but provides a more comprehensive feature set. Its performance is generally acceptable for most applications, though it may not be as fast as simpler libraries.

API Consistency

  • dot-prop: dot-prop has a simple and consistent API focused solely on property access and manipulation, making it easy to learn and use for straightforward tasks.
  • lodash.get: lodash.get follows the Lodash convention of chaining and method consistency, which is beneficial if you are already familiar with other Lodash functions.
  • lodash.set: lodash.set maintains the same API style as lodash.get, ensuring a consistent experience when accessing and modifying properties within objects.
  • object-path: object-path offers a consistent API for both getting and setting properties, with additional methods for deletion and existence checks, making it versatile for various use cases.

Use Cases

  • dot-prop: dot-prop is ideal for simple applications where you need to access or modify properties without the overhead of a larger library. It is perfect for quick tasks and small projects.
  • lodash.get: lodash.get is best suited for projects that already utilize Lodash, providing a familiar interface for developers. It is great for applications requiring safe property access in complex data structures.
  • lodash.set: lodash.set is useful in applications that need to update nested object properties dynamically, especially when combined with lodash.get for safe access.
  • object-path: object-path is suitable for complex applications that require comprehensive property manipulation capabilities, including getting, setting, and deleting properties in a flexible manner.
How to Choose: dot-prop vs lodash.get vs lodash.set vs object-path
  • dot-prop: Choose dot-prop if you need a lightweight solution for simple property access and manipulation, particularly when working with dot-notation strings. It is minimalistic and efficient for straightforward use cases.
  • lodash.get: Select lodash.get if you are already using Lodash in your project or require a robust solution that includes additional utility functions. It provides a safe way to access nested properties without throwing errors if a property is undefined.
  • lodash.set: Opt for lodash.set if you need to set values in deeply nested objects safely and efficiently, especially in conjunction with lodash.get. It is ideal for scenarios where you want to update object properties dynamically based on variable paths.
  • object-path: Consider object-path if you require a comprehensive library that supports both getting and setting properties with dot-notation and bracket-notation, along with additional features like deleting properties. It is suitable for more complex object manipulations.
README for dot-prop

dot-prop

Get, set, or delete a property from a nested object using a dot path

Install

npm install dot-prop

Usage

import {getProperty, setProperty, hasProperty, deleteProperty} from 'dot-prop';

// Getter
getProperty({foo: {bar: 'unicorn'}}, 'foo.bar');
//=> 'unicorn'

getProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep');
//=> undefined

getProperty({foo: {bar: 'a'}}, 'foo.notDefined.deep', 'default value');
//=> 'default value'

getProperty({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot');
//=> 'unicorn'

getProperty({foo: [{bar: 'unicorn'}]}, 'foo[0].bar');
//=> 'unicorn'

// Setter
const object = {foo: {bar: 'a'}};
setProperty(object, 'foo.bar', 'b');
console.log(object);
//=> {foo: {bar: 'b'}}

const foo = setProperty({}, 'foo.bar', 'c');
console.log(foo);
//=> {foo: {bar: 'c'}}

setProperty(object, 'foo.baz', 'x');
console.log(object);
//=> {foo: {bar: 'b', baz: 'x'}}

setProperty(object, 'foo.biz[0]', 'a');
console.log(object);
//=> {foo: {bar: 'b', baz: 'x', biz: ['a']}}

// Has
hasProperty({foo: {bar: 'unicorn'}}, 'foo.bar');
//=> true

// Deleter
const object = {foo: {bar: 'a'}};
deleteProperty(object, 'foo.bar');
console.log(object);
//=> {foo: {}}

object.foo.bar = {x: 'y', y: 'x'};
deleteProperty(object, 'foo.bar.x');
console.log(object);
//=> {foo: {bar: {y: 'x'}}}

API

getProperty(object, path, defaultValue?)

Get the value of the property at the given path.

Returns the value if any.

setProperty(object, path, value)

Set the property at the given path to the given value.

Returns the object.

hasProperty(object, path)

Check whether the property at the given path exists.

Returns a boolean.

deleteProperty(object, path)

Delete the property at the given path.

Returns a boolean of whether the property existed before being deleted.

escapePath(path)

Escape special characters in a path. Useful for sanitizing user input.

import {getProperty, escapePath} from 'dot-prop';

const object = {
	foo: {
		bar: '👸🏻 You found me Mario!',
	},
	'foo.bar' : '🍄 The princess is in another castle!',
};
const escapedPath = escapePath('foo.bar');

console.log(getProperty(object, escapedPath));
//=> '🍄 The princess is in another castle!'

deepKeys(object)

Returns an array of every path. Non-empty plain objects and arrays are deeply recursed and are not themselves included.

This can be useful to help flatten an object for an API that only accepts key-value pairs or for a tagged template literal.

import {getProperty, deepKeys} from 'dot-prop';

const user = {
	name: {
		first: 'Richie',
		last: 'Bendall',
	},
	activeTasks: [],
	currentProject: null
};

for (const property of deepKeys(user)) {
	console.log(`${property}: ${getProperty(user, property)}`);
	//=> name.first: Richie
	//=> name.last: Bendall
	//=> activeTasks: []
	//=> currentProject: null
}

Sparse arrays are supported. In general, avoid using sparse arrays.

object

Type: object | array

Object or array to get, set, or delete the path value.

You are allowed to pass in undefined as the object to the get and has functions.

path

Type: string

Path of the property in the object, using . to separate each nested key.

Use \\. if you have a . in the key.

The following path components are invalid and results in undefined being returned: __proto__, prototype, constructor.

value

Type: unknown

Value to set at path.

defaultValue

Type: unknown

Default value.