New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@flatten/array

Package Overview
Dependencies
Maintainers
2
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@flatten/array

Quickly flatten an array in-place.

  • 1.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.4K
decreased by-23.88%
Maintainers
2
Weekly downloads
 
Created
Source

@flatten/array

Build Status Dependency Status npm version Coverage Status

Quickly flatten an array in-place.

View many implementation variations and use it to run benchmarks. This implementation is named inplace2.js there.

Run npm run perf to compare the performance of this implementation to both array-flatten and flatten-array.

Install

npm install @flatten/array --save

Usage

var flatten = require('@flatten/array')

console.log(flatten([1, [2, 3], [4, [5, [6, 7]]]]))
// [ 1, 2, 3, 4, 5, 6, 7 ]

// NOTE: it's an in-place change to the array supplied.
var array = [ 1, [2, 3], 4, [5, [6, [7], 8], 9], 10 ]
flatten(array)
// array = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
// no need to assign array to it.
// but, you can, if you want to because it returns it
// to allow inline use.

Performance

tl;dr This is the fastest flatten implementation.

An in-place flatten on the top-most array is significantly faster than producing a new array with the flattened results. Run this project's performance test to see it compared to both array-flatten and flatten-array.

Also, use my fork of the array-flatten project to compare this implementation, called inplace2.js there, against many other implementations.

Normally it's an anti-pattern to alter a provided array unless it is the specific intent (such as a sort utility). In this case, it is the specific intent, the fastest implementation, and the common use pattern.

The third reason, "common use pattern", means it's common to create a new array which contains many other things which may, or may not, be arrays. Then, that top-most array is provided to @flatten/array. So, it's a brand new array created to contain the results and is therefore a perfect candidate to mutate to hold the final results.

For example:

buildSomething([ // top-most array is a great target
  // all these may or may not provide arrays
  require('some-plugin'),
  require('./local-addons'),
  [ 'something', 'defined', 'here' ]
  makeSomeMore()
])

Performance results screenshot shows this implementation is significantly faster than the other two:

Show performance comparison with various inputs for this implementation, array-flatten, and flatten-array.

MIT License

Keywords

FAQs

Package last updated on 23 Apr 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc