Skip to content

Commit

Permalink
feat: better parallelization
Browse files Browse the repository at this point in the history
Sync speed improvements
Better isolation of benchmark tests
Drop of unnecessary undefined variable initializations
Separate replace function for type string regex variable
Strict check for eimptiness of input, content or output variables
Benchmark testing is now part of test suite
README benchmark section update
Better parallelization
Async uses fast-glob streams to speed up replacing
Replace async available in benchmark test suite
README benchmark section update
Benchmark test 'input as glob pattern' fix
Better cross-platform process spawn handling in benchmarks
Npm test script refactor

fixes #17, #10, #19
  • Loading branch information
FRSgit authored Oct 21, 2019
1 parent 735785d commit 4d90537
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 167 deletions.
26 changes: 23 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
dist: trusty
sudo: required
language: node_js
node_js:
- "node"
- "lts/*"
cache: yarn
matrix:
include:
- name: "Standard linting"
script: yarn standard
node_js: "lts/*"
- name: "Unit tests & coverage"
script: yarn test
node_js:
- "node"
- "lts/*"
- name: "Benchmark test - glob"
script: travis_wait yarn test:benchmark:glob
node_js:
- "node"
- "lts/*"
- name: "Benchmark test - string"
script: yarn test:benchmark:string
node_js:
- "node"
- "lts/*"
113 changes: 67 additions & 46 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# FRS-replace

[![NPM version](https://img.shields.io/npm/v/frs-replace.svg?style=flat)](https://www.npmjs.com/package/frs-replace)
[![Build Status](https://travis-ci.org/FRSource/FRS-replace.svg?branch=master)](https://travis-ci.org/FRSource/FRS-replace)
[![Coverage Status](https://coveralls.io/repos/github/FRSource/FRS-replace/badge.svg?branch=master)](https://coveralls.io/github/FRSource/FRS-replace?branch=master)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release) [![Greenkeeper badge](https://badges.greenkeeper.io/FRSource/FRS-replace.svg)](https://greenkeeper.io/)
[![codebeat badge](https://codebeat.co/badges/5496a006-a13d-48cc-baeb-37c79a1f6444)](https://codebeat.co/projects/github-com-frsource-frs-replace-master)
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)

# FRS-replace

CLI & Node wrapper around [javascript replace](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) which allows on-the-fly replacing (with or without changing input files), [globbing](https://en.wikipedia.org/wiki/Glob_(programming)), [piping](https://en.wikipedia.org/wiki/Pipeline_(Unix)) and many more!
The fastest ([see benchmarks](#benchmarks)) CLI & Node wrapper around [javascript replace](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) which allows on-the-fly replacing (with or without changing input files), [globbing](https://en.wikipedia.org/wiki/Glob_(programming)), [piping](https://en.wikipedia.org/wiki/Pipeline_(Unix)) and many more!

* [Installation](#installation)
* [Node API usage](#node-api-usage)
Expand All @@ -16,17 +16,20 @@ CLI & Node wrapper around [javascript replace](https://developer.mozilla.org/en-
* [Benchmarks](#benchmarks)

## Installation
##### yarn
```
$ yarn add frs-replace
```

##### npm
yarn

```bash
yarn add frs-replace
```
$ npm install frs-replace

npm

```bash
npm install frs-replace
```

##### download
download
[zipped from FRS-replace Releases](https://github.com/FRSource/FRS-replace/releases)

## Node API usage
Expand Down Expand Up @@ -61,14 +64,16 @@ Where `/* options */` is an object containing:
FRS-replace <regex> <replacement> [options]
```

#### Positionals:
### Positionals

| Option | Type | Description |
| --- | --- | --- |
| \<regex\> | string | First parameter to [RegExp constructor](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp#Syntax) |
| \<replacement\> | string | String or path to replacement function file (see &#8209;&#8209;replace&#8209;fn switch for details) |

#### Options:
> Note: Every boolean option can be negated with use of `--no-` prefix, e.g. `--stdout` or `--no-stdout` turn stdout output on or off, respectively.
### Options

> Note: Every boolean option can be negated with use of `--no-` prefix, e.g. `--stdout` or `--no-stdout` turn stdout output on or off, respectively.
> Note: Object types can be set using [dot notation](https://github.com/yargs/yargs-parser#dot-notation). So, e.g. if you want to pass `utf8` value under i-read-opts encoding field you should write `--i-read-opts.encoding utf8`.
Expand All @@ -91,9 +96,9 @@ FRS-replace <regex> <replacement> [options]

> Note: while most of examples is using synchronous API method, in all cases `.async` is applicable as well.
#### 1. Replace all `a` occurences with `b` from given `foo.js` and returns result / writes result to console :
### 1. Replace all `a` occurences with `b` from given `foo.js` and returns result / writes result to console

###### API
#### 1.1 API

```javascript
const FRSReplace = require('FRS-replace')
Expand Down Expand Up @@ -127,14 +132,16 @@ const resultAsync = await FRSReplace.async({

```

###### CLI
#### 1.2 CLI

```bash
FRS-replace a b -i foo.js --stdout
```

#### 2. Replace all `a` occurences with `b` from given `foo.js` and save result to `foo_replaced.js` :
### 2. Replace all `a` occurences with `b` from given `foo.js` and save result to `foo_replaced.js`

#### 2.1 API

###### API
```javascript
const result = require('FRS-replace').sync({
input : 'foo.js',
Expand All @@ -144,14 +151,16 @@ const result = require('FRS-replace').sync({
})
```

###### CLI
#### 2.2 CLI

```bash
FRS-replace a b -i foo.js -o foo_replaced.js
```

#### 3. Replace all `a` occurences with `b` from given array of files and save result to `foo_replaced.js` using default `\n` as result-joining string :
### 3. Replace all `a` occurences with `b` from given array of files and save result to `foo_replaced.js` using default `\n` as result-joining string

#### 3.1 API

###### API
```javascript
const result = require('FRS-replace').sync({
input : ['foo.js', 'foo2.js'],
Expand All @@ -161,22 +170,24 @@ const result = require('FRS-replace').sync({
})
```

###### CLI
#### 3.2 CLI

```bash
FRS-replace a b -i foo.js foo2.js -o foo_replaced.js --i-join-str "\n/////\n"
```

*or*
or

```bash
FRS-replace a b -i foo.js -i foo2.js -o foo_replaced.js --i-join-str "\n/////\n"
```

> Note: Arrays can be passed under single flag-entry as a space-separated list *or* under same flag repeated multiple times (all values will be concatenated into single array using, details - [yargs array notation](https://github.com/yargs/yargs-parser#dot-notation)).
#### 4. Replace all `a` occurences with `b` from all `.js` files in `foo` directory and save result to `foo_replaced.js` using `\n/////\n` as result-joining string :
### 4. Replace all `a` occurences with `b` from all `.js` files in `foo` directory and save result to `foo_replaced.js` using `\n/////\n` as result-joining string

#### 4.1 API

###### API
```javascript
const result = require('FRS-replace').sync({
input : 'foo/*.js',
Expand All @@ -187,14 +198,16 @@ const result = require('FRS-replace').sync({
})
```

###### CLI
#### 4.2 CLI

```bash
FRS-replace a b -i foo/*.js -o foo_replaced.js --i-join-str "\n/////\n"
```

#### 5. Replace all `a` occurences with `b` in given content string `abcd` and save result to `foo_replaced.js`
### 5. Replace all `a` occurences with `b` in given content string `abcd` and save result to `foo_replaced.js`

#### 5.1 API

###### API
```javascript
const result = require('FRS-replace').sync({
content : 'abcd',
Expand All @@ -204,48 +217,56 @@ const result = require('FRS-replace').sync({
})
```

###### CLI
#### 5.2 CLI

```bash
FRS-replace a b --content abcd -o foo_replaced.js
```

#### 6. Replace all `a` occurences with `b` from piped stream and save it to output file:
### 6. Replace all `a` occurences with `b` from piped stream and save it to output file

#### 6.1 CLI

###### CLI
```bash
<read-file> | FRS-replace a b > <output-file-path>
```

#### 7. Replaces all `a` occurences with `b` from piped stream and pass it through `stdout` stream to next command

###### CLI
### 7. Replaces all `a` occurences with `b` from piped stream and pass it through `stdout` stream to next command

#### 7.1 CLI

```bash
<read-file> | FRS-replace a b | <next-command>
```

#### 8. Both pipe & options styles can be mixed together, here - getting input from `i` argument and passing output down the stream to next command
### 8. Both pipe & options styles can be mixed together, here - getting input from `i` argument and passing output down the stream to next command

#### 8.1 CLI

###### CLI
```bash
FRS-replace a b -i foo.js | <next-command>
```

## Benchmarks
#### input as glob pattern [1000 iterations x 100 repetitions]
## Benchmarks (Node v10.16.0)

### input as glob pattern [40 files x 1000 iterations x 100 repetitions]

| Library (best&nbsp;bolded) | Execution time [s] | Difference percentage (comparing&nbsp;to&nbsp;best&nbsp;time) |
| --- | --- | --- |
| **FRS-replace async** | 0.36640944 | 0.0000% |
| FRS-replace sync | 0.39553770 | 7.9496% |
| replace-in-file | 1.78587186 | 387.3979% |
| FRS-replace async | 0.01761663 | 103.7503% |
| **FRS-replace sync** | 0.00864619 | 0.0000% |
| replace-in-file | 0.02154322 | 149.1644% |
| replace async | *N/A* | *N/A* |
| replace sync | 0.44655926 | 21.8744% |
| replace sync | 0.05026399 | 481.3428% |
| replace-string | *N/A* | *N/A* |
#### input & replacement as strings [1000 iterations x 100 repetitions]

### input & replacement as strings [1000 iterations x 100 repetitions]

| Library (best&nbsp;bolded) | Execution time [s] | Difference percentage (comparing&nbsp;to&nbsp;best&nbsp;time) |
| --- | --- | --- |
| FRS-replace async | 0.01015828 | 59.0095% |
| FRS-replace sync | 0.00657347 | 2.8957% |
| FRS-replace async | 0.00011756 | 215.1822% |
| **FRS-replace sync** | 0.00003730 | 0.0000% |
| replace-in-file | *N/A* | *N/A* |
| replace async | *N/A* | *N/A* |
| replace sync | *N/A* | *N/A* |
| **replace-string** | 0.00638847 | 0.0000% |
| replace-string | 0.00004905 | 31.5049% |
Loading

0 comments on commit 4d90537

Please sign in to comment.