Skip to content

Conversation

@robinvdvleuten
Copy link

Hi there! By using an array reduce on the key, you'll can go even leaner with 124 bytes 💪

@kevlened
Copy link

Unfortunately, after minification, using reduce is actually larger (using https://jscompress.com/ which uses UglifyJS 3 and Babili):

original (119 bytes):
export default function dlv(a,b,c,d){for(d=0,b=b.split?b.split('.'):b;a&&d<b.length;)a=a[b[d++]];return a===void 0?c:a}

with reduce (123 bytes):
export default function dlv(a,b,c){return(a=(b.split?b.split('.'):b).reduce(function(d,e){return d&&d[e]},a))===void 0?c:a}

@developit
Copy link
Owner

Thanks for the great explanation @kevlened! This also jives with my experience. Worth noting that performance is also at play here, and reduce() has fairly poor performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants