Skip to content

Commit c81921f

Browse files
author
bjarneo
committed
remove a loop which makes the code even more performant
1 parent b3464ae commit c81921f

File tree

4 files changed

+11
-40
lines changed

4 files changed

+11
-40
lines changed

dist/extract-domain.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

index.js

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function getDomainFromUrl(url) {
1818

1919
// Find offset of the domain
2020
while (len-- && ++i) {
21-
if (domainInc && url[i] === '/') {
21+
if (domainInc && (url[i] === '/' || url[i] === ':')) {
2222
break;
2323
}
2424

@@ -31,6 +31,8 @@ function getDomainFromUrl(url) {
3131
offsetDomain = i;
3232
}
3333

34+
offsetPath = i;
35+
3436
i = offsetDomain;
3537

3638
// Find offset before domain name.
@@ -45,38 +47,11 @@ function getDomainFromUrl(url) {
4547
break;
4648
}
4749

48-
i = offsetDomain;
49-
50-
// Get the offset path
51-
while (i++) {
52-
if (i >= url.length) {
53-
break;
54-
}
55-
56-
// If we hit the port, set the offsetPath and break the loop
57-
if (url[i] === ':') {
58-
offsetPath = i;
59-
60-
break;
61-
}
62-
63-
// Continue until we find the start of a path
64-
if (url[i] !== '/') {
65-
continue;
66-
}
67-
68-
offsetPath = i;
69-
70-
break;
71-
}
72-
7350
// offsetStartSlice should always be larger than protocol
7451
if (offsetStartSlice < 6) {
7552
return '';
7653
}
7754

78-
// It has been a wild ride
79-
// .. slice
8055
// Tried several approaches slicing a string. Can't get it any faster than this.
8156
return url.slice(offsetStartSlice, offsetPath);
8257
}

index.test.js

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,17 @@ const urls = [
99
'http://example.com:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument',
1010
'http://www.so.many.sub.domains.example.com:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument',
1111
'http://user:[email protected]:80/path/to/myfile.html?key1=value1&key2=value2#SomewhereInTheDocument',
12-
'ftp://example.org/resource.txt'
12+
'ftp://example.org/resource.txt',
13+
'http://www.npmjs.com'
1314
];
1415

15-
const expected = [
16-
'npmjs.com',
17-
'example.com',
18-
'npmjs.com',
19-
'example.org'
20-
];
16+
const expected = [ 'npmjs.com', 'example.com', 'npmjs.com', 'example.org' ];
2117

2218
describe('extract domain', () => {
2319
it('should extract given domain from string', () => {
24-
assert.equal(extractDomain(urls[1]), expected[1]);
25-
2620
assert.equal(extractDomain(urls[0]), expected[0]);
21+
assert.equal(extractDomain(urls[1]), expected[1]);
22+
assert.equal(extractDomain(urls[7]), expected[0]);
2723
});
2824

2925
it('should extract given domain from an array of strings', () => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "extract-domain",
3-
"version": "1.0.1",
3+
"version": "1.1.0",
44
"description": "Extract domain from given string",
55
"main": "dist/extract-domain.min.js",
66
"author": "Bjarne Oeverli",

0 commit comments

Comments
 (0)