Skip to content

Commit

Permalink
refactor: reduced library kb
Browse files Browse the repository at this point in the history
  • Loading branch information
Zizzamia committed Apr 23, 2020
1 parent c0bcdce commit bc0e782
Show file tree
Hide file tree
Showing 13 changed files with 135 additions and 134 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<img src="https://github.com/Zizzamia/perfume.js/blob/master/docs/src/assets/perfume-logo-v4-5-0.png" align="left" width="200" />
</a>

# [Perfume.js v5.0.0-rc.6](http://perfumejs.com)
# [Perfume.js v5.0.0-rc.7](http://perfumejs.com)

[![NPM version](https://badge.fury.io/js/perfume.js.svg)](https://www.npmjs.org/package/perfume.js) [![Build Status](https://travis-ci.org/Zizzamia/perfume.js.svg?branch=master)](https://travis-ci.org/Zizzamia/perfume.js) [![NPM Downloads](http://img.shields.io/npm/dm/perfume.js.svg)](https://www.npmjs.org/package/perfume.js) [![Test Coverage](https://api.codeclimate.com/v1/badges/f813d2f45b274d93b8c5/test_coverage)](https://codeclimate.com/github/Zizzamia/perfume.js/test_coverage) [![JS gzip size](https://img.badgesize.io/https://unpkg.com/perfume.js?compression=gzip&label=JS+gzip+size)](https://unpkg.com/perfume.js)

Expand Down Expand Up @@ -223,7 +223,6 @@ Perfume helps expose all PerformanceResourceTiming entries and group data data c
```javascript
const perfume = new Perfume({
resourceTiming: true,
dataConsumption: true,
analyticsTracker: ({ metricName, data }) => {
myAnalyticsTool.track(metricName, data);
})
Expand Down Expand Up @@ -275,8 +274,6 @@ Default options provided to Perfume.js constructor.

```javascript
const options = {
// Metrics
dataConsumption: false,
resourceTiming: false,
// Analytics
analyticsTracker: options => {},
Expand Down
9 changes: 9 additions & 0 deletions __tests__/config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { config } from '../src/config';

describe('config', () => {
it('should be defined', () => {
expect(config.logPrefix).toEqual('Perfume.js:');
expect(config.logging).toEqual(true);
expect(config.maxMeasureTime).toEqual(15000);
});
});
25 changes: 25 additions & 0 deletions __tests__/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { config } from '../src/config';
import { logWarn } from '../src/log';

describe('log', () => {
let spy;

describe('.logWarn()', () => {
it('should throw a console.warn if config.warning is true', () => {
spy = jest.spyOn(window.console, 'warn');
logWarn('message');
expect(spy.mock.calls.length).toEqual(1);
expect(spy).toHaveBeenCalledWith(config.logPrefix, 'message');
});

/**
it('should not throw a console.warn if config.logging is false', () => {
spy = jest.spyOn(window.console, 'warn');
config.logging = false;
logWarn('message');
expect(spy.mock.calls.length).toEqual(0);
expect(spy).not.toHaveBeenCalled();
});
*/
});
});
86 changes: 18 additions & 68 deletions __tests__/perfume.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { config } from '../src/config';
import { C, W, WN, WP } from '../src/constants';
import * as log from '../src/log';
import Perfume from '../src/perfume';
import mock from './_mock';

Expand All @@ -23,35 +25,16 @@ describe('Perfume', () => {
}
});

describe('config', () => {
const instance = new Perfume();

it('should be defined', () => {
expect(instance.config.dataConsumption).toEqual(false);
expect(instance.config.resourceTiming).toEqual(false);
expect(instance.config.logPrefix).toEqual('Perfume.js:');
expect(instance.config.logging).toEqual(true);
expect(instance.config.maxMeasureTime).toEqual(15000);
});
});

describe('constructor', () => {
it('should run with config version A', () => {
new Perfume({
dataConsumption: true,
resourceTiming: true,
});
});

it('should run with config version B', () => {
new Perfume({
dataConsumption: true,
resourceTiming: true,
});
});

it('should run with config version C', () => {
new Perfume({
dataConsumption: true,
logging: false,
});
});
Expand All @@ -70,7 +53,7 @@ describe('Perfume', () => {
});

it('should throw a logWarn if recording already started', () => {
spy = jest.spyOn(perfume as any, 'logWarn');
spy = jest.spyOn(log, 'logWarn');
perfume.start('metricName');
perfume.start('metricName');
expect(spy.mock.calls.length).toEqual(1);
Expand All @@ -80,15 +63,15 @@ describe('Perfume', () => {

describe('.end()', () => {
it('should throw a logWarn if param is correct and recording already stopped', () => {
spy = jest.spyOn(perfume as any, 'logWarn');
spy = jest.spyOn(log, 'logWarn');
perfume.end('metricName');
expect(spy.mock.calls.length).toEqual(1);
expect(spy).toHaveBeenCalledWith('Recording already stopped.');
});

it('should call log() with correct params', () => {
spy = jest.spyOn(perfume as any, 'log');
perfume.config.logging = true;
config.logging = true;
perfume.start('metricName');
perfume.end('metricName');
expect(spy.mock.calls.length).toEqual(1);
Expand Down Expand Up @@ -117,7 +100,7 @@ describe('Perfume', () => {

it('should call sendTiming() with correct params', () => {
spy = jest.spyOn(perfume as any, 'sendTiming');
perfume.config.logging = true;
config.logging = true;
perfume.start('metricName');
perfume.end('metricName');
expect(spy.mock.calls.length).toEqual(1);
Expand Down Expand Up @@ -185,14 +168,14 @@ describe('Perfume', () => {

describe('.log()', () => {
it('should not call window.console.log() if logging is disabled', () => {
perfume.config.logging = false;
config.logging = false;
spy = jest.spyOn(C, 'log');
(perfume as any).log({ metricName: '', data: '0 ms' });
expect(spy).not.toHaveBeenCalled();
});

it('should call window.console.log() if logging is enabled', () => {
perfume.config.logging = true;
config.logging = true;
spy = jest.spyOn(C, 'log');
(perfume as any).log({
measureName: 'metricName',
Expand All @@ -206,7 +189,7 @@ describe('Perfume', () => {
});

it('should call window.console.log() if params are correct', () => {
perfume.config.logging = true;
config.logging = true;
spy = jest.spyOn(C, 'log');
(perfume as any).log({
measureName: 'metricName',
Expand All @@ -221,7 +204,7 @@ describe('Perfume', () => {

it('should call window.console.log() with data', () => {
const data = {};
perfume.config.logging = true;
config.logging = true;
spy = jest.spyOn(C, 'log');
(perfume as any).log({
measureName: 'metricName',
Expand Down Expand Up @@ -445,7 +428,7 @@ describe('Perfume', () => {
});

it('should call initResourceTiming when resourceTiming or is true', () => {
(perfume as any).config.resourceTiming = true;
config.resourceTiming = true;
spy = jest.spyOn(perfume as any, 'initResourceTiming');
(perfume as any).initPerformanceObserver();
expect(spy.mock.calls.length).toEqual(1);
Expand All @@ -455,7 +438,7 @@ describe('Perfume', () => {

describe('.initResourceTiming()', () => {
beforeEach(() => {
perfume.config.dataConsumption = true;
config.resourceTiming = true;
(perfume as any).perfObservers.dataConsumption = { disconnect: () => {} };
});

Expand Down Expand Up @@ -536,23 +519,6 @@ describe('Perfume', () => {
});
});

describe('.logWarn()', () => {
it('should throw a console.warn if config.warning is true', () => {
spy = jest.spyOn(window.console, 'warn');
(perfume as any).logWarn('message');
expect(spy.mock.calls.length).toEqual(1);
expect(spy).toHaveBeenCalledWith(perfume.config.logPrefix, 'message');
});

it('should not throw a console.warn if config.logging is false', () => {
spy = jest.spyOn(window.console, 'warn');
perfume.config.logging = false;
(perfume as any).logWarn('message');
expect(spy.mock.calls.length).toEqual(0);
expect(spy).not.toHaveBeenCalled();
});
});

describe('.getNavigatorInfo()', () => {
it('when navigator is not supported should return an empty object', () => {
(WN as any) = undefined;
Expand Down Expand Up @@ -616,7 +582,6 @@ describe('Perfume', () => {
beforeEach(() => {
perfume = new Perfume({
...mock.defaultPerfumeConfig,
resourceTiming: true,
});
});

Expand All @@ -626,7 +591,7 @@ describe('Perfume', () => {
fetch: 0,
total: 0,
};
perfume.config.dataConsumption = true;
config.resourceTiming = true;
(perfume as any).perfObservers.dataConsumption = { disconnect: () => {} };
});

Expand Down Expand Up @@ -695,33 +660,18 @@ describe('Perfume', () => {
});
});

describe('.pushTask()', () => {
it('should call cb() if requestIdleCallback is undefined', () => {
spy = jest.fn();
perfume['pushTask'](spy);
expect(spy.mock.calls.length).toEqual(1);
});

it('should call requestIdleCallback if is defined', () => {
spy = jest.fn();
(W as any).requestIdleCallback = spy;
perfume['pushTask'](() => {});
expect(spy.mock.calls.length).toEqual(1);
});
});

describe('.sendTiming()', () => {
it('should not call analyticsTracker() if isHidden is true', () => {
perfume.config.analyticsTracker = () => {};
spy = jest.spyOn(perfume.config, 'analyticsTracker');
config.analyticsTracker = () => {};
spy = jest.spyOn(config, 'analyticsTracker');
perfume['isHidden'] = true;
(perfume as any).sendTiming({ measureName: 'metricName', data: 123 });
expect(spy).not.toHaveBeenCalled();
});

it('should call analyticsTracker() if analyticsTracker is defined', () => {
perfume.config.analyticsTracker = () => {};
spy = jest.spyOn(perfume.config, 'analyticsTracker');
config.analyticsTracker = () => {};
spy = jest.spyOn(config, 'analyticsTracker');
(perfume as any).sendTiming({ measureName: 'metricName', data: 123 });
expect(spy.mock.calls.length).toEqual(1);
expect(spy).toHaveBeenCalledWith({
Expand Down
21 changes: 21 additions & 0 deletions __tests__/utils.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { W } from '../src/constants';
import { pushTask } from '../src/utils';

describe('utils', () => {
let spy;

describe('.pushTask()', () => {
it('should call cb() if requestIdleCallback is undefined', () => {
spy = jest.fn();
pushTask(spy);
expect(spy.mock.calls.length).toEqual(1);
});

it('should call requestIdleCallback if is defined', () => {
spy = jest.fn();
(W as any).requestIdleCallback = spy;
pushTask(() => {});
expect(spy.mock.calls.length).toEqual(1);
});
});
});
4 changes: 1 addition & 3 deletions docs/src/app/app.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ <h3 class="box-title" id="/total-blocking-time/">
<div class="box">
<h3 class="box-title" id="/resource-timing/">
<a href="{{ path }}#/resource-timing/">
Resource Timing (experimental)
Resource Timing
</a>
</h3>
<p>Resource Timing collects performance metrics for document-dependent resources. Stuff like style sheets, scripts, images, et cetera.</p>
Expand All @@ -292,7 +292,6 @@ <h3 class="box-title" id="/resource-timing/">
<td class="code">
<pre><span class="red">const</span> <span class="blue">perfume</span> <span class="red">= new</span> <span class="violet">Perfume</span>({{'{'}}
resourceTiming<span class="red">:</span> <span class="blue">true</span>,
dataConsumption<span class="red">:</span> <span class="blue">true</span>,
<span class="violet">analyticsTracker</span><span class="red">:</span> ({{'{'}} metricName, data {{'}'}}) => {{'{'}}
myAnalyticsTool.<span class="violet">track</span>(metricName, data);
{{'}'}})
Expand Down Expand Up @@ -361,7 +360,6 @@ <h3 class="box-title" id="/default-options/">
<td class="code">
<pre><span class="red">const</span> <span class="blue">options</span> <span class="red">=</span> {{'{'}}
<span class="gray">// Metrics</span>
dataConsumption<span class="red">:</span> <span class="blue">false</span>,
resourceTiming<span class="red">:</span> <span class="blue">false</span>,
<span class="gray">// Analytics</span>
analyticsTracker<span class="red">:</span> options => {{'{}'}},
Expand Down
2 changes: 0 additions & 2 deletions docs/src/app/perfume.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ export function analyticsTracker(options) {
}

export const PerfumeConfig = {
cumulativeLayoutShift: true,
dataConsumption: true,
resourceTiming: true,
analyticsTracker,
};

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "perfume.js",
"version": "5.0.0-rc.6",
"version": "5.0.0-rc.7",
"description": "Tiny web performance monitoring library which reports field data back to your favorite analytics tool.",
"keywords": [
"performance",
Expand Down Expand Up @@ -90,7 +90,7 @@
"global": {
"branches": 82,
"functions": 100,
"lines": 94,
"lines": 93,
"statements": 94
}
},
Expand Down
10 changes: 10 additions & 0 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { IPerfumeConfig } from './types';

export const config: IPerfumeConfig = {
// Metrics
resourceTiming: false,
// Logging
logPrefix: 'Perfume.js:',
logging: true,
maxMeasureTime: 15000,
};
13 changes: 13 additions & 0 deletions src/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { config } from './config';
import { C } from './constants';

/**
* Ensures console.warn exist and logging is enable for
* warning messages
*/
export const logWarn = (message: string): void => {
if (!config.logging) {
return;
}
C.warn(config.logPrefix, message);
}
Loading

0 comments on commit bc0e782

Please sign in to comment.