Skip to content

Commit

Permalink
Working on perf options
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason3S committed Oct 1, 2018
1 parent 68c0f1e commit 563c3ce
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 30 deletions.
13 changes: 12 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [

{
"type": "node",
"request": "launch",
Expand All @@ -13,6 +14,16 @@
"outDir": "${workspaceRoot}/lib",
"sourceMaps": true
},
{
"type": "node",
"request": "launch",
"name": "Run Current File",
"program": "${file}",
"cwd": "${workspaceRoot}",
"outDir": "${workspaceRoot}/lib",
"sourceMaps": true,
"preLaunchTask": "npm: build"
},
{
"type": "node",
"request": "attach",
Expand All @@ -25,7 +36,7 @@
// Name of configuration; appears in the launch configuration drop down menu.
"name": "Run mocha",
// Type of configuration. Possible values: "node", "mono".
"type": "node2",
"type": "node",
// Request type "launch" or "attach"
"request": "launch",
// Workspace relative or absolute path to the program.
Expand Down
66 changes: 37 additions & 29 deletions src/Seq.perf.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as test from 'tape';
import * as GS from './GenSequence';
import {from} from './Seq';
import * as ops from './operators';
import {filter, reduce, skip, take, first, concatMap, map} from './operators';

test('Simple Generator to an array', (t) => {
Expand Down Expand Up @@ -28,7 +29,6 @@ test('filter filter reduce', (t) => {
.filter(a => !!(a & 2))
.reduce((a, b) => a + b);
};

const fnSeq = () => {
return from(getValues()).pipe(
filter(a => !!(a & 1)),
Expand Down Expand Up @@ -93,39 +93,46 @@ test('filter slice filter reduce', (t) => {
t.end();
});

test('filter slice filter reduce (1000)', (t) => {
const testName = 'filter slice filter reduce (1000)';
test('filter slice filter reduce (2000)', (t) => {
const testName = 'filter slice filter reduce (2000)';
const count = 2000;
const cntSkip = 100;
const cntTake = 50;
const fn1 = (a: number) => !!(a & 1);
const fn2 = (a: number) => !!(a & 2);
const fnR = (a: number, b: number) => a + b;
const getValues = () => range(0, 2000);
const fnBase = () => {
return [...getValues()]
.filter(a => !!(a & 1))
.slice(100)
.slice(0,500)
.filter(a => !!(a & 2))
.reduce((a, b) => a + b);
.filter(fn1)
.slice(cntSkip)
.filter(fn2)
.slice(0,cntTake)
// .map(a => (console.log('array ' + a), a))
.reduce(fnR, 0);
};

const fnSeq = () => {
return from(getValues()).pipe(
filter(a => !!(a & 1)),
skip(100),
take(500),
filter(a => !!(a & 2)),
reduce((a, b) => a + b),
).toValue();
}
const seqPipe = ops.pipe(
filter(fn1),
skip(cntSkip),
filter(fn2),
take(cntTake),
// ops.tap(a => console.log('seq ' + a)),
reduce(fnR, 0),
);
const fnSeq = () => from(getValues()).pipe(seqPipe).toValue();
const fnExp = () => {
return GS.genSequence(getValues())
.filter(a => !!(a & 1))
.skip(100)
.take(500)
.filter(a => !!(a & 2))
.reduce((a, b) => a + b);
}
.filter(fn1)
.skip(cntSkip)
.filter(fn2)
.take(cntTake)
.reduce(fnR, 0);
};
const measurements = [
measure('base', fnBase, 1000),
measure('seq', fnSeq, 1000),
measure('gen', fnExp, 1000),
measure('base', fnBase, count),
measure('seq', fnSeq, count),
measure('gen', fnExp, count),
];

measurements.forEach(m => t.equals(m.result, measurements[0].result));
Expand All @@ -134,6 +141,7 @@ test('filter slice filter reduce (1000)', (t) => {
});

test('filter slice filter first (1000)', (t) => {
const count = 1000;
const testName = 'filter slice filter first (1000)';
const getValues = () => range(0, 1000);
const fnBase = () => {
Expand Down Expand Up @@ -166,9 +174,9 @@ test('filter slice filter first (1000)', (t) => {
.first();
}
const measurements = [
measure('base', fnBase, 1000),
measure('seq', fnSeq, 1000),
measure('gen', fnExp, 1000),
measure('base', fnBase, count),
measure('seq', fnSeq, count),
measure('gen', fnExp, count),
];

measurements.forEach(m => t.equals(m.result, measurements[0].result));
Expand Down
9 changes: 9 additions & 0 deletions src/operators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export function concatMap<T, U>(fn: (v: T) => IterableLike<U>): OperatorFunction
return (iter: IterableLike<T>) => iop.concatMap(iter, fn);
}

export function tap<T>(fn: (v: T) => void): OperatorFunction<T, T> {
return function *(iter: IterableLike<T>) {
for (const t of iter) {
fn(t);
yield t;
}
}
}

/**
*
* Filter Operators
Expand Down

0 comments on commit 563c3ce

Please sign in to comment.