Skip to content

Commit bd5b864

Browse files
authored
Merge pull request #60 from mattaylor/prefix_methods
Prefix methods
2 parents 0802ac9 + de54b89 commit bd5b864

File tree

11 files changed

+562
-313
lines changed

11 files changed

+562
-313
lines changed

README.md

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ Objix is a delightfully convienient, high performance, zero dependency and super
44

55
The functions are all non enumerable and include copies of Object class methods and Array prototype methods applied to the values of the object as well others to delete keys, stringify, promisify, memoize, compare, split/join objects, check types, log messages and trapping/observing property assignments.
66

7-
This library is highly optimised with zero copy operations where possible. The source is only 3.5kb (2.6kb minified) which allows for fast loading and easy integration without additional compilation or tree shaking. There is however limited type checking to guard against unwanted side effects, and there may be some undiscovered edge case that do not behave as expected. Performance in most cases is significantly faster than `lodash` equivalents especially when working with small objects. For example `ob.map(fn)` is typically over 65% faster than `_.mapValues(ob, fn)` and some operations such as `pick` can be several thousand times quicker according to simple [benchmarks](docs/bench.md).
7+
This library is highly optimised with zero copy operations where possible. The source is only 3.8kb (2.8kb minified) which allows for fast loading and easy integration without additional compilation or tree shaking. Performance in most cases is significantly faster than `lodash` equivalents especially when working with small objects. For example `ob._map(fn)` is typically over 65% faster than `_.mapValues(ob, fn)` and some operations such as `pick` can be several thousand times quicker according to simple [benchmarks](docs/bench.md).
88

99
Interactive docs and demos are availble on https://objix.dev/#/docs/api.
1010

11+
## Upgrading from 1.0
12+
13+
Objix 2.0 now prefixes all prototype methods with '_'. This avoids name clashes with built in methods and reduces unwanted side effects and compatibility issues inherent in the 1.x releases.
14+
15+
The `_size()` method is now renamied to `_len()`.
16+
1117
## Getting Started - Node
1218

1319
- Install:
@@ -20,7 +26,7 @@ Interactive docs and demos are availble on https://objix.dev/#/docs/api.
2026

2127
```javascript
2228
require('objix')
23-
var o = { a: 1 }.map(v => v + 1).log()
29+
var o = { a: 1 }._map(v => v + 1).log()
2430
```
2531

2632
## Getting Started - Browser
@@ -29,7 +35,7 @@ Interactive docs and demos are availble on https://objix.dev/#/docs/api.
2935
<script src="https://cdn.jsdelivr.net/gh/mattaylor/objix@main/objix.min.js"></script>
3036

3137
<script>
32-
var o = { a: 1 }.map(v => v + 1).log()
38+
var o = { a: 1 }._map(v => v + 1)._log()
3339
</script>
3440
```
3541

@@ -65,7 +71,7 @@ The following methods are availble to all Objects via protoype inheritence, unle
6571
| [`split`](docs/api.md#split) | Split `this` into multiple objects from array property values |
6672
| [`contains`](docs/api.md#contains) | Check if `this` contains all entries from another object to a given depth. |
6773
| [`eq`](docs/api.md#eq) | Compare key and value identity between `this` and other objects to a given depth |
68-
| [`size`](docs/api.md#size) | Return number of entres in `this`. |
74+
| [`len`](docs/api.md#len) | Return number of entres in `this`. |
6975
| [`keyBy`](docs/api.md#keyBy) | Re-index values of this `this` using a given key path |
7076
| [`memo`](docs/api.md#memo) | Memoize `this` as a function with configurable result cache expiration |
7177
| [`bind`](docs/api.md#bind) | Assign a function as a method of `this` with optional memoization |
@@ -83,10 +89,10 @@ Most of these function return objects including those modifying `this` and so ca
8389

8490
```javascript
8591
var o = { a: 0, b: 1, c: 2 }
86-
.filter(v => v > 0)
87-
.log('POSITIVE') // 2022-10-07T00:00 POSITIVE { b: 1, c: 2 }
88-
.map(v => v + 1)
89-
.log('INCREMENT') // 2022-10-07T00:00 INCREMENT { b: 2, c: 3 }
92+
._filter(v => v > 0)
93+
._log('POSITIVE') // 2022-10-07T00:00 POSITIVE { b: 1, c: 2 }
94+
._map(v => v + 1)
95+
._log('INCREMENT') // 2022-10-07T00:00 INCREMENT { b: 2, c: 3 }
9096
```
9197

9298
</div>
@@ -97,8 +103,8 @@ All functions documented below are also callable with a '\_' prefix to the funct
97103
This can help ensure that the function is callable when overwritten by other object property assignments.
98104

99105
```javascript
100-
var o = { a: 1 }.size() == { a: 1 }._size() //true
101-
var o = { a: 1 }.find(v => v) == { a: 1 }._find(v => v) //true
106+
var o = { a: 1 }._len() == { a: 1 }._len() //true
107+
var o = { a: 1 }._find(v => v) == { a: 1 }._find(v => v) //true
102108
```
103109

104110
## Simple Classes

bench.js

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ Calculate run time for *heats* batches of *iters* executions of each function, w
1515
Each batch run also includes a 100 iteration warmup verifying the results of the function against objix
1616
*/
1717
function compare(funcs) {
18-
let hist = funcs.map(v => null), start
19-
for (let r = 0; r < heats; r++) for (let [key,fun] of _.shuffle(funcs.entries())) if (fun) {
18+
let hist = funcs._map(v => null), start
19+
for (let r = 0; r < heats; r++) for (let [key,fun] of _.shuffle(funcs._entries())) if (fun) {
2020
for (let i = 0; i < 100; i++) assert.deepEqual(funcs.objix(), fun(), fun)
2121
if (!hist[key]) hist[key] = hdr.build()
2222
start = performance.now()
@@ -31,7 +31,7 @@ function compare(funcs) {
3131
[k + ' Inc', round((100*v.mean/hist.lodash.mean)-100)],
3232
])
3333
*/
34-
let res = hist.map(v => v?.mean)
34+
let res = hist._map(v => v?.mean)
3535
res['% Inc'] = round(100*(hist.objix.mean - hist.lodash.mean)/hist.lodash.mean)
3636
//res['% Err'] = round(100*(hist.objix.stddev + hist.lodash.stddev)/(hist.objix.mean + hist.lodash.mean))
3737
return res
@@ -41,12 +41,12 @@ function report(title, ob) {
4141
console.log(title)
4242
console.table({
4343
Map: {
44-
objix : () => ob.map(v => v+1),
44+
objix : () => ob._map(v => v+1),
4545
lodash: () => _.mapValues(ob, v => v+1),
4646
vanilla: () => Object.fromEntries(Object.entries(ob).map(([k,v]) => [k, v+1])),
4747
},
4848
Pick: {
49-
objix: () => ob.pick(v => v == 1),
49+
objix: () => ob._pick(v => v == 1),
5050
lodash: () => _.pickBy(ob, v => v == 1),
5151
vanilla: () => Object.fromEntries(Object.entries(ob).flatMap(([k,v]) => v == 1 ? [[k,v]] : [])),
5252
},
@@ -58,12 +58,12 @@ function report(title, ob) {
5858
},
5959
*/
6060
Find: {
61-
objix: () => ob.find(v => v == 1),
61+
objix: () => ob._find(v => v == 1),
6262
lodash: () => _.findKey(ob, v => v == 1),
6363
vanilla: () => { for (let [k,v] of Object.entries(ob)) if (v == 1) return k },
6464
},
6565
FlatMap: {
66-
objix: () => ob.flatMap((k,v) => [[k,v],[k+1, v+1]]),
66+
objix: () => ob._flatMap((k,v) => [[k,v],[k+1, v+1]]),
6767
lodash: () => Object.fromEntries(_.flatMap(ob, (v,k) => [[k,v],[k+1, v+1]])),
6868
/*
6969
_lodash: () => {
@@ -75,45 +75,45 @@ function report(title, ob) {
7575
//vanilla: () => { for (let [k,v] of Object.entries(ob)) if (v == 1) return k },
7676
},
7777
Has: {
78-
objix: () => ob.has(3),
78+
objix: () => ob._has(3),
7979
lodash: () => _.includes(ob, 3),
8080
vanilla: () => Object.values(ob).includes(3)
8181
},
8282
KeyBy: {
83-
objix: () => [{a:1},{a:2},{a:3}].keyBy('a'),
83+
objix: () => [{a:1},{a:2},{a:3}]._keyBy('a'),
8484
lodash: () => _.keyBy([{a:1},{a:2},{a:3}], 'a'),
8585
},
8686
Equals: {
87-
objix: () => ob.eq(ob.clone(), -1),
88-
lodash: () => _.isEqual(ob, ob.clone()),
89-
vanilla: () => { try { return assert.deepEqual(ob,ob.clone()) || true } catch { return false }},
87+
objix: () => ob._eq(ob._clone(), -1),
88+
lodash: () => _.isEqual(ob, ob._clone()),
89+
vanilla: () => { try { return assert.deepEqual(ob,ob._clone()) || true } catch { return false }},
9090
},
9191
Clone: {
92-
objix: () => ob.clone(),
92+
objix: () => ob._clone(),
9393
lodash: () => _.clone(ob),
9494
vanilla: () => Object.assign({}, ob), //No Construcotrs!
9595
},
9696
Deep: {
97-
objix: () => ob.clone(-1),
97+
objix: () => ob._clone(-1),
9898
vanilla: typeof structuredClone !== 'undefined' ? () => structuredClone(ob) : null,
9999
lodash: () => _.cloneDeep(ob),
100100
},
101101
Extend: {
102-
objix: () => ob.extend({a: 1, b: 2, c: 3}),
102+
objix: () => ob._extend({a: 1, b: 2, c: 3}),
103103
lodash: () => _.defaults(ob, {a: 1, b:2, c: 2}),
104104
vanilla: () => Object.assign({}, {a: 1, b: 2, c: 3}, ob)
105105
},
106106
Some: {
107-
objix: () => ob.some(v => v == 'x'),
107+
objix: () => ob._some(v => v == 'x'),
108108
vanilla: () => Object.values(ob).some(v => v == 'x'),
109109
lodash: () => _.some(_.values(ob), v => v == 'x'),
110110
},
111111
Every: {
112-
objix: () => ob.every(v => v),
112+
objix: () => ob._every(v => v),
113113
lodash: () => _.every(_.values(ob), v => v),
114114
vanilla: () => Object.values(ob).every(v => v),
115115
}
116-
}.map(compare))
116+
}._map(compare))
117117
}
118118
const d1 = new Date()
119119
const d2 = new Date()

bun-repl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('hello')

0 commit comments

Comments
 (0)