0.0.35 • Published 6 years ago

object-assign-properties v0.0.35

Weekly downloads
2
License
ISC
Repository
github
Last release
6 years ago

object-assign-properties

npm

npm

curried and reusable function define object properties like Object.defineProperties

Usage & Arguments

objectAssignProperties(descriptor[, properties[, object]])

((descriptor, properties, object) => *) => descriptor => properties => object => object

there are 3 arguments of this curried function.

  • descriptor description for properties (description)

  • properties properties with it's value

  • object target object

example

const objectAssignProperties = require("object-assign-properties");

const objectAssignPropertiesReadonly = objectAssignProperties({
    writable:false,
    configurable:false
})

getter/setter

getter and setter description would be little different from built-in ones

because of objectAssignProperties always assign mutable properties for mutable objects, so we need more property infos in our accessor functions

you can define getter and setter like this example below:

const objectAssignProperties = require("object-assign-properties");

const objectAssignPropertiesWithAccessor = objectAssignProperties({
   get(lastValue, key, self) { // inital or last value will save in a interal scope
       return lastValue + 1;
   }
   set(lastValue, newValue, prop, self) {
       if (typeof newValue === "number") {
          return newValue; // return value would be set as a new value for the property
       } else {
          return lastValue;
       }
   }
})

Example

normal description

const objectAssignProperties = require("object-assign-properties");

let target = {};

objectAssignProperties({
    writable:false,
    configurable:false
}, {
    a:1
    b:2
}, target);

console.log(target); // assign readonly properties `a` `b` `c` to target object

normal description (curry)

const objectAssignProperties = require("object-assign-properties");

const objectAssignPropertiesReadonly = objectAssignProperties({
    writable:false,
    configurable:false
});

const objectAssignPropertiesReadonlyABC = objectAssignPropertiesReadonly({
    a:1, b:2, c:3
})

let target1 = {};
let target2 = {};

objectAssignPropertiesReadonlyABC(target1); // assign readonly properties `a` `b` `c` to target object
objectAssignPropertiesReadonlyABC(target2);

accessor description (getter and setter)

const objectAssignProperties = require("object-assign-properties");

const objectAssignPropertiesGetPlus1 = objectAssignProperties({
    get(value, key){
        return value + 1;
    }
}, {
    a:1, b:2, c:"c"
})

let target1 = {};
let target2 = {};

objectAssignPropertiesGetPlus1(target1);
objectAssignPropertiesGetPlus1(target2);

console.log(target1.a) // 2;
console.log(target1.b) // 3;
console.log(target1.c) // "c1";

console.log(target2.a) // 2;
console.log(target2.b) // 3;
console.log(target2.c) // "c1";

Benchmark (nodejs v7.3.0 | macOS | 2.2 GHz Intel Core i7)

Benchmark sources can be found in the folder

1.create assign properties for object

assign a,b,c, with enumerable:false and writable:false

function \ ops/seccreate
Object.defineProperties530,060
object-assign-properties223,242
object-assign-properties curry 1 argument219,352
object-assign-properties curry 2 arguments215,466

2.call assigned properties with accessor (getter and setter)

assigned a property with getter and setter, then object[property] = object[property] + 1

function \ ops/seccall first objectcall second objectcall third object
object-assign-properties60,629,79027,843,14226,903,146
Object.defineProperties77,280,4143,825,4803,832,452

why object-assign-properties performance better after the first object ?

reference issue nodejs/help|#442

objects with the same property name and different function defined via accessor (getter/setter) (even with the same return value) are always turned to dictionary mode expect the first one, however if we always define same function for accessor of the same property name, then the object will stay fast!

Reference

dependencies

0.0.35

6 years ago

0.0.34

6 years ago

0.0.33

6 years ago

0.0.32

6 years ago

0.0.31

6 years ago

0.0.3

7 years ago

0.0.29

7 years ago

0.0.28

7 years ago

0.0.27

7 years ago

0.0.26

7 years ago

0.0.25

7 years ago

0.0.24

7 years ago

0.0.23

7 years ago

0.0.22

7 years ago

0.0.21

7 years ago

0.0.2

7 years ago

0.0.15

7 years ago

0.0.14

7 years ago

0.0.13

7 years ago

0.0.12

7 years ago

0.0.11

7 years ago

0.0.1

7 years ago