Skip to content

Commit

Permalink
es5 vs esnext
Browse files Browse the repository at this point in the history
  • Loading branch information
jamiemccrindle committed Jun 27, 2018
1 parent b810400 commit 2febd84
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
lib/
node_modules/
esnext/
es5/
*.log
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,19 @@ A library of async iterator extensions for JavaScript
Async iterators are a useful way to handle asynchronous streams. This library adds a number
of utility methods similar to those found in lodash, underscore or Ramda.

# es5 vs esnext

Axax contains both transpiled es5 code as well as esnext code, the difference being that
esnext uses the native for await syntax. In nodejs 10.x that gives approximately a 40% speedup.

# Examples

## fromEvents

```fromEvents``` turns DOM events into an iterable.

```javascript
import { fromEvents } from "axax/fromEvents";
import { fromEvents } from "axax/es5/fromEvents";

const clicks = fromEvents(document, 'click');

Expand All @@ -33,7 +38,7 @@ for await (const click of clicks) {
is essentially how ```fromEvents``` was implemented.

```javascript
import { DeferredIterable } from "axax/deferredIterable";
import { DeferredIterable } from "axax/es5/deferredIterable";

const deferredIterable = new DeferredIterable();

Expand Down
48 changes: 24 additions & 24 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ You typically interact with `DeferredIterable` in 2 ways:
### Example

```javascript
import { DeferredIterable } from "axax/deferredIterable";
import { DeferredIterable } from "axax/es5/deferredIterable";

const deferredIterable = new DeferredIterable();

Expand All @@ -62,7 +62,7 @@ The callback to call to send a `IteratorResult` into the `DeferredIterable`. An
has a `done` boolean property and a `value` property.

```javascript
import { DeferredIterable } from "axax/deferredIterable";
import { DeferredIterable } from "axax/es5/deferredIterable";

const deferredIterable = new DeferredIterable();
deferredIterable.callback({ done: false, value: 1 });
Expand All @@ -80,7 +80,7 @@ for await (const item of deferredIterable.iterable) {
An ```AsyncIterable``` that returns values supplied by calling ```callback```.

```javascript
import { DeferredIterable } from "axax/deferredIterable";
import { DeferredIterable } from "axax/es5/deferredIterable";

const deferredIterable = new DeferredIterable();
deferredIterable.callback({ done: false, value: 1 });
Expand Down Expand Up @@ -118,7 +118,7 @@ Construct a new async iterable from a series
of values.

```javascript
import { of } from "axax/of";
import { of } from "axax/es5/of";

const values = of(1, 2, 3);

Expand All @@ -133,8 +133,8 @@ Go through each item in the iterable and run a mapping function. The result will
iterable with the transformed values.

```javascript
import { map } from "axax/map";
import { of } from "axax/of";
import { map } from "axax/es5/map";
import { of } from "axax/es5/of";

const mapped = map(value => value * 2)(of(1, 2, 3));

Expand All @@ -149,8 +149,8 @@ Go through each item in the iterable and run a mapping function that returns an
result is then flattened.

```javascript
import { flatMap } from "axax/flatMap";
import { of } from "axax/of";
import { flatMap } from "axax/es5/flatMap";
import { of } from "axax/es5/of";

const mapped = flatMap(
async function* (value) {
Expand All @@ -169,8 +169,8 @@ for await(const item of mapped) {
Filter an iterable based on some criteria.
```javascript
import { filter } from "axax/filter";
import { of } from "axax/of";
import { filter } from "axax/es5/filter";
import { of } from "axax/es5/of";

const filtered = filter(
value => value % 2 === 0
Expand All @@ -186,8 +186,8 @@ for await(const item of filtered) {
Concatenate 2 iterables in order
```javascript
import { concat } from "axax/concat";
import { of } from "axax/of";
import { concat } from "axax/es5/concat";
import { of } from "axax/es5/of";

const concatted = concat(
of(1, 2)
Expand All @@ -205,8 +205,8 @@ reduced by a function that compbines a running total or accumulator with
the next value to produce the new total or accumulator.
```javascript
import { reduce } from "axax/reduce";
import { of } from "axax/of";
import { reduce } from "axax/es5/reduce";
import { of } from "axax/es5/of";

const reduced = concat(
(accumulator, next) => accumulator + next, // sum the values together
Expand All @@ -221,8 +221,8 @@ console.log(reduced); // 6
Merge a number of async iterators into one concurrently. Order is not important.
```javascript
import { merge } from "axax/merge";
import { of } from "axax/of";
import { merge } from "axax/es5/merge";
import { of } from "axax/es5/of";

const merged = merge(
of(1, 2), of(3, 4)
Expand All @@ -238,8 +238,8 @@ for await(const item of merged) {
Insert values at the beginning of an async iterable.
```javascript
import { insert } from "axax/insert";
import { of } from "axax/of";
import { insert } from "axax/es5/insert";
import { of } from "axax/es5/of";

const inserted = insert(
1, 2, 3
Expand All @@ -258,8 +258,8 @@ result of the function. Typically used for side effects like
logging.
```javascript
import { tap } from "axax/tap";
import { of } from "axax/of";
import { tap } from "axax/es5/tap";
import { of } from "axax/es5/of";

const tapped = tap(
value => console.log(value) // prints 1, 2, 3
Expand All @@ -275,8 +275,8 @@ for await(const item of concatted) {
Creates a new iterable out of the two supplied by pairing up equally-positioned items from both iterables. The returned iterable is truncated to the length of the shorter of the two input iterables.
```javascript
import { zip } from "axax/zip";
import { of } from "axax/of";
import { zip } from "axax/es5/zip";
import { of } from "axax/es5/of";

const zipped = zip(
of(1, 2)
Expand All @@ -296,8 +296,8 @@ for await(const item of concatted) {
then set to 0.
```javascript
import { range } from "axax/range";
import { of } from "axax/of";
import { range } from "axax/es5/range";
import { of } from "axax/es5/of";

const ranged = range(1, 3);

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
},
"scripts": {
"test": "jest",
"build": "tsc"
"build": "tsc -p tsconfig.es5.json && tsc -p tsconfig.esnext.json"
},
"main": "lib/index.js",
"types": "lib/index.d.ts"
"main": "es5/index.js",
"types": "es5/index.d.ts"
}
24 changes: 24 additions & 0 deletions performance.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { range } = require("./lib/range");
const { map } = require("./lib/map");
const { filter } = require("./lib/filter");

async function time(promise) {
const start = Date.now();
await promise();
return Date.now() - start;
}

async function run() {
const timing = await time(async function() {
let count = 0;
for await (const item of filter(value => value > -1)(
map(value => value)(range(1000000))
)) {
count += 1;
}
console.log(count);
});
console.log(timing + "ms");
}

run().catch(e => console.log(e));
7 changes: 7 additions & 0 deletions tsconfig.es5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "es5",
"outDir": "./es5"
}
}
8 changes: 8 additions & 0 deletions tsconfig.esnext.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "esnext",
"outDir": "./esnext",
"module": "commonjs"
}
}

0 comments on commit 2febd84

Please sign in to comment.