Skip to content

Commit

Permalink
Fix: Compression on Trie nodes that are prefixes of another
Browse files Browse the repository at this point in the history
  • Loading branch information
sukratkashyap authored and nicjansma committed Sep 17, 2019
1 parent 381c5d2 commit 59ea4cd
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 30 deletions.
44 changes: 22 additions & 22 deletions dist/usertiming-compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@
// nothing exists yet, create either a leaf if this is the end of the word,
// or a branch if there are letters to go
cur = cur[letter] = (i === (letters.length - 1) ? value : {});
} else if (typeof node === "string") {
} else if (typeof node === "string" || typeof node === "number") {
// this is a leaf, but we need to go further, so convert it into a branch
cur = cur[letter] = { "!": node };
} else if (i === (letters.length - 1)) {
Expand Down Expand Up @@ -635,26 +635,26 @@
}
};

//
// Export to the appropriate location
//
if (typeof define === "function" && define.amd) {
//
// AMD / RequireJS
//
define([], function() { // eslint-disable-line strict
return UserTimingCompression; // eslint-disable-line no-undef
});
} else if (typeof module !== "undefined" && module.exports) {
//
// Node.js
//
module.exports = UserTimingCompression; // eslint-disable-line no-undef
} else if (typeof root !== "undefined") {
//
// Browser Global
//
root.UserTimingCompression = UserTimingCompression; // eslint-disable-line no-undef, no-underscore-dangle
}
//
// Export to the appropriate location
//
if (typeof define === "function" && define.amd) {
//
// AMD / RequireJS
//
define([], function() { // eslint-disable-line strict
return UserTimingCompression; // eslint-disable-line no-undef
});
} else if (typeof module !== "undefined" && module.exports) {
//
// Node.js
//
module.exports = UserTimingCompression; // eslint-disable-line no-undef
} else if (typeof root !== "undefined") {
//
// Browser Global
//
root.UserTimingCompression = UserTimingCompression; // eslint-disable-line no-undef, no-underscore-dangle
}

}(typeof window !== "undefined" ? window : undefined));
14 changes: 7 additions & 7 deletions dist/usertiming-compression.vanilla.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@
// nothing exists yet, create either a leaf if this is the end of the word,
// or a branch if there are letters to go
cur = cur[letter] = (i === (letters.length - 1) ? value : {});
} else if (typeof node === "string") {
} else if (typeof node === "string" || typeof node === "number") {
// this is a leaf, but we need to go further, so convert it into a branch
cur = cur[letter] = { "!": node };
} else if (i === (letters.length - 1)) {
Expand Down Expand Up @@ -635,11 +635,11 @@
}
};

if (typeof root !== "undefined") {
//
// Browser Global
//
root.UserTimingCompression = UserTimingCompression; // eslint-disable-line no-undef, no-underscore-dangle
}
if (typeof root !== "undefined") {
//
// Browser Global
//
root.UserTimingCompression = UserTimingCompression; // eslint-disable-line no-undef, no-underscore-dangle
}

}(typeof window !== "undefined" ? window : undefined));
2 changes: 1 addition & 1 deletion src/usertiming-compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@
// nothing exists yet, create either a leaf if this is the end of the word,
// or a branch if there are letters to go
cur = cur[letter] = (i === (letters.length - 1) ? value : {});
} else if (typeof node === "string") {
} else if (typeof node === "string" || typeof node === "number") {
// this is a leaf, but we need to go further, so convert it into a branch
cur = cur[letter] = { "!": node };
} else if (i === (letters.length - 1)) {
Expand Down
44 changes: 44 additions & 0 deletions test/data/21.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"compressed": {
"mark1": 59,
"mark12": 104,
"mark_after_bar": 123,
"mark_before_bar": 456,
"mark_before_foo": 111
},
"uri": "~(mark~(1~(*21~59~2~104)~_~(after_bar~123~before_~(bar~456~foo~111))))",
"zlib": false,
"msgpack": false,
"entries": [
{
"duration": 0,
"entryType": "mark",
"name": "mark1",
"startTime": 189
},
{
"duration": 0,
"entryType": "mark",
"name": "mark12",
"startTime": 1300
},
{
"duration": 0,
"entryType": "mark",
"name": "mark_before_foo",
"startTime": 1333
},
{
"duration": 0,
"entryType": "mark",
"name": "mark_before_bar",
"startTime": 5370
},
{
"duration": 0,
"entryType": "mark",
"name": "mark_after_bar",
"startTime": 1371
}
]
}
48 changes: 48 additions & 0 deletions test/test-usertiming-compression.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@
duration: 5
}])).to.deep.equal({ "mark1": 1, "measure1": "3_5" });
});

it("should compress mark and should have integer values", function() {
expect(utc.compressUserTiming([{
entryType: "mark",
name: "abc",
startTime: 189
}, {
entryType: "measure",
name: "abcd",
startTime: 1300
}])).to.deep.equal({ "abc": 59, "abcd": 104 });
});
});

//
Expand Down Expand Up @@ -226,6 +238,21 @@
};
expect(utc.convertToTrie(data)).to.eql(expected);
});

it("should be able to convert to a trie when value is an integer", function() {
var data = { "abc": 59, "abcd": 104 };
var expected = {
"a": {
"b": {
"c": {
"!": 59,
"d": 104
}
}
}
};
expect(utc.convertToTrie(data)).to.eql(expected);
});
});

//
Expand Down Expand Up @@ -272,6 +299,27 @@

expect(utc.optimizeTrie(trie, true)).to.eql(expected);
});

it("should optimize a tree with integer values", function() {
var trie = {
"a": {
"b": {
"c": {
"!": 59,
"d": 104
}
}
}
};
var expected = {
"abc":
{
"!": 59,
"d": 104
}
};
expect(utc.optimizeTrie(trie, true)).to.eql(expected);
});
});

//
Expand Down

0 comments on commit 59ea4cd

Please sign in to comment.