convert tree structure

JavaScript performance comparison

Test case created by ideadapt

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

      
<script>
Benchmark.prototype.setup = function() {
  const data = [
      {
          id: 1234,
          name: 'Aufnahme 5',
          date: '20181107',
          tracks: [
              {
                  id: 1324,
                  trackNumber: 1,
                  name: 'track 1',
                  channels: [
                      {
                          id: 31,
                          channelNr: 1,
                          name: 'Gesang Robin',
                          size: 2345345,
                      },
                      {
                          id: 33,
                          channelNr: 2,
                          name: 'Gesang Beni',
                          size: 243233,
                      },
                      {
                          id: 35,
                          channelNr: 3,
                          name: 'Gesang Silvan',
                          size: 987987,
                      },
                  ],
              },
          ],
      },
      {
          id: 2345,
          name: 'Aufnahme 3',
          date: '20181123',
          tracks: [
              {
                  id: 1324,
                  trackNumber: 1,
                  name: 'track 1',
                  channels: [
                      {
                          id: 31,
                          channelNr: 1,
                          name: 'Gesang Robin',
                          size: 2345345,
                      },
                      {
                          id: 33,
                          channelNr: 2,
                          name: 'Gesang Beni',
                          size: 243233,
                      },
                      {
                          id: 35,
                          channelNr: 3,
                          name: 'Gesang Silvan',
                          size: 987987,
                      },
                  ],
              },
          ],
      },
      {
          id: 3456,
          name: 'Aufnahme 2',
          date: '20170803',
          tracks: [
              {
                  id: 1324,
                  trackNumber: 1,
                  name: 'track 1',
                  channels: [
                      {
                          id: 21,
                          channelNr: 1,
                          name: 'Gesang Robin',
                          size: 2345345,
                      },
                      {
                          id: 23,
                          channelNr: 2,
                          name: 'Gesang Beni',
                          size: 243233,
                      },
                      {
                          id: 25,
                          channelNr: 3,
                          name: 'Gesang Silvan',
                          size: 987987,
                      },
                  ],
              },
          ],
      },
      {
          id: 4567,
          name: 'Aufnahme 1',
          date: '20170803',
          tracks: [
              {
                  id: 1324,
                  trackNumber: 1,
                  name: 'track 1',
                  channels: [
                      {
                          id: 11,
                          channelNr: 1,
                          name: 'Gesang Robin',
                          size: 2345345,
                      },
                      {
                          id: 13,
                          channelNr: 2,
                          name: 'Gesang Beni',
                          size: 243233,
                      },
                      {
                          id: 15,
                          channelNr: 3,
                          name: 'Gesang Silvan',
                          size: 987987,
                      },
                  ],
              },
          ],
      },
      {
          id: 5678,
          name: 'Aufnahme 4',
          date: '20171013',
          tracks: [
              {
                  id: 1324,
                  trackNumber: 1,
                  name: 'track 1',
                  channels: [
                      {
                          id: 41,
                          channelNr: 1,
                          name: 'Gesang Robin',
                          size: 2345345,
                      },
                      {
                          id: 43,
                          channelNr: 2,
                          name: 'Gesang Beni',
                          size: 243233,
                      },
                      {
                          id: 45,
                          channelNr: 3,
                          name: 'Gesang Silvan',
                          size: 987987,
                      },
                  ],
              },
          ],
      },
  ];

};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
v1
"use strict";
const exports = {};
Object.defineProperty(exports, "__esModule", { value: true });
var FolderType;
(function (FolderType) {
    FolderType[FolderType["PLAIN"] = 0] = "PLAIN";
    FolderType[FolderType["RECORDING"] = 1] = "RECORDING";
    FolderType[FolderType["TRACK"] = 2] = "TRACK";
})(FolderType = exports.FolderType || (exports.FolderType = {}));
class RecordingsToFileTreeConverter {
    convert(recordings) {
        const recordingsPerYear = new Map();
        _.sortBy(recordings, ['date', 'name']).forEach(recording => {
            const year = this.toDate(recording.date)
                .getFullYear()
                .toString();
            if (!recordingsPerYear.has(year)) {
                recordingsPerYear.set(year, {
                    filename: year,
                    type: 0,
                    children: [],
                });
            }
            const formatted = this.formatDate(this.toDate(recording.date));
            recordingsPerYear.get(year).children.push({
                type: 1,
                filename: `${formatted} ${recording.name}`,
                children: _.sortBy(recording.tracks, 'trackNumber').map(track => {
                    return {
                        type: 2,
                        filename: `${track.trackNumber}. ${track.name}`,
                        id: track.id,
                        children: _.sortBy(track.channels, 'channelNr').map(channel => {
                            return {
                                filename: channel.name,
                                file: Object.assign({}, channel),
                            };
                        }),
                    };
                }),
            });
        });
        return [...recordingsPerYear.values()];
    }
    toDate(str) {
        return new Date(str.slice(0, 4), Number(str.slice(4, 6)) - 1, str.slice(6, 8));
    }
    formatDate(date) {
        const monthNames = [
            'Januar',
            'Februar',
            'März',
            'April',
            'Mai',
            'Juni',
            'Juli',
            'August',
            'September',
            'Oktober',
            'November',
            'Dezember',
        ];
        return `${date.getDate()}. ${monthNames[date.getMonth()]}`;
    }
}
exports.RecordingsToFileTreeConverter = RecordingsToFileTreeConverter;

new RecordingsToFileTreeConverter().convert(data);
pending…
v2
"use strict";
const exports = {};
    Object.defineProperty(exports, "__esModule", { value: true });
    var FolderType;
    (function (FolderType) {
        FolderType[FolderType["PLAIN"] = 0] = "PLAIN";
        FolderType[FolderType["RECORDING"] = 1] = "RECORDING";
        FolderType[FolderType["TRACK"] = 2] = "TRACK";
    })(FolderType = exports.FolderType || (exports.FolderType = {}));
    var RecordingListUtils = /** @class */ (function () {
        function RecordingListUtils() {
        }
        RecordingListUtils.buildFileTree = function (recordings) {
            var _this = this;
            return _.chain(recordings)
                .sortBy(['date', 'name'])
                .map(function (rec) { return ({
                filename: RecordingListUtils.extractYear(rec),
                folderType: FolderType.PLAIN,
                children: new Array({
                    filename: RecordingListUtils.extractDayMonth(rec) + " " + rec.name,
                    folderType: FolderType.RECORDING,
                    children: _this.createTrackList(rec)
                })
            }); })
                .reduce(this.groupByName, [])
                .tap(function (folderTree) { return folderTree.forEach(function (yearFolder) {
                yearFolder.children = yearFolder.children.reduce(_this.groupByName, []);
            }); })
                .value();
        };
        RecordingListUtils.createTrackList = function (rec) {
            var _this = this;
            return _.chain(rec.tracks)
                .sortBy('track.trackNumber')
                .map(function (track) { return ({
                filename: track.trackNumber + ". " + track.name,
                id: track.id,
                folderType: FolderType.TRACK,
                children: _this.createChannelList(track)
            }); })
                .value();
        };
        RecordingListUtils.createChannelList = function (track) {
            return _.chain(track.channels)
                .sortBy('file.channelNr')
                .map(function (channel) { return ({
                filename: channel.name,
                file: channel
            }); })
                .value();
        };
        RecordingListUtils.groupByName = function (groupedFolders, currFolder) {
            var folderFound = groupedFolders.find(function (folder) { return folder.filename === currFolder.filename; });
            if (folderFound) {
                currFolder.children.forEach(function (rec) { return folderFound.children.push(rec); });
            }
            else {
                groupedFolders.push(currFolder);
            }
            return groupedFolders;
        };
        RecordingListUtils.extractYear = function (recording) {
            return recording.date.substring(0, 4);
        };
        RecordingListUtils.extractDayMonth = function (recording) {
            return moment(recording.date, 'YYYYMMDD').format('Do MMMM');
        };
        return RecordingListUtils;
    }());
    exports.RecordingListUtils = RecordingListUtils;
    RecordingListUtils.buildFileTree(data);
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 Comments