score:2
This is the expected behaviour.
If you look at the source code for d3.bisector
, you'll see that it does not iterate the whole array the way you think. Have a look here:
while (lo < hi) {
var mid = lo + hi >>> 1;
if (compare(a[mid], x) > 0) hi = mid;
else lo = mid + 1;
}
The interesting part lies here: var mid = lo + hi >>> 1;
. What this bitwise (named zero-fill right shift) does is calculating the midpoint of the array.
In your case, the array has 78 elements. Therefore:
console.log(0 + 78 >>> 1)
And it keeps getting the midpoint of the remaining part, again and again, until finding the element. Have a look here:
let lo = 0,
hi = 78;
while (lo < hi) {
let mid = lo + hi >>> 1;
console.log(mid)
lo = mid + 1;
}
For an array of 78 elements it does that 6 times (not 5, as you mentioned), and that's why you see the console.log
working only 6 times.
Have a look at this demo, an array of 78 elements, the console.log
works 7 times:
const data = d3.range(78);
const bisect = d3.bisector(function(d) {
console.log(d);
return d;
}).left;
bisect(data, 5);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
Now let's increase the array to 1000 elements. The console.log
works 10 times:
const data = d3.range(1000);
const bisect = d3.bisector(function(d) {
console.log(d);
return d;
}).left;
bisect(data, 5);
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/5.7.0/d3.min.js"></script>
Source: stackoverflow.com
Related Query
- Bisecting an array of objects: are not all of them enumerated?
- Two array of objects are merging, but they're not sorting - D3
- d3 click coordinates are relative to page not svg - how to translate them (Chrome error)
- How can I use D3.js to take an array of Javascript objects use them in a line graph?
- pie() does not accept an array of objects
- D3.js: "On the fly" added elements to array are not refreshing the svg graphic
- JavaScript ensure all numbers are unique and if not plus one (or more)
- d3 enter().append() not appending all elements of my array
- Chart is not showing the data array of objects
- group by and aggregate across all keys an array of objects
- Not all d3 points are showing in correct position on leaflet map
- D3 - not all children nodes are shown at the same time
- Drawing a line using d3 is not visible when all data items are the same
- Not all coordinates are plotted using parallel-coordinates (d3)
- Why are not all my text labels showing in my Scatterplot?
- D3.js array of objects not creating elements
- How can I access all keys and values in an array of objects without looping
- D3: ordinal scale not working with array of objects
- How to make sure that all data are pushed to the array before resolving promise?
- getting the array length the same across all objects
- JavaScript "Undefined is not a function" when looping through an array of objects
- Javascript: Array of Objects is returning undefined when accessing to a single Element. Even though there are 696 Objects saved in it
- d3 bisector not working with array of objects
- When loading .json in d3.js i'm not getting an array of Objects
- How do I remove all children elements from a node and then apply them again with different color and size?
- How to get maximum value from an array of objects to use in d3.scale.linear().domain()
- MultiBar chart with nvd3 / d3 only shows labels for every other tick on the x-axis. How can I get them all to show up?
- How do you access an array of Objects using D3?
- d3: A sub array of objects
- Why are .domain, tickFormat and tickValues not recognised inside dimensions variable? (d3, parallel coordinates)
More Query from same tag
- d3.js circles are not appearing
- Filter D3 multidimensional array by another array
- Display text and direct via hyperlink on d3 Map
- D3 Apply projection to d3-tip tooltip
- D3.js not updating properly data from socket.io
- Transitioning sunburst in D3.js
- Clockwise direction for RadarChart
- SVG tag mousedown event with D3
- D3.js - Appending to <div> not working
- Implementing zoom buttons using d3
- Convert JSON text to JS object
- Cannot add text to d3 horizontal bars
- c3 time series scatter chart, time data in y axis
- d3 graph - How do I make the JS get the JSON from PHP script and then select axes and plot?
- d3.js v4 programmatic Pan+Zoom. How?
- D3JS - Combining "rect" functions
- How to draw multiple self-edges in a node-link diagram in D3
- Loosen spine, smooth line in D3js
- How to display paths/lines when I'm hovering circles instead of Voronoi cells?
- transition of x-axis results in overflow
- Wait for d3 to load
- Update attributes on nested selections
- JavaScript - Bar Graph with custom animation fill
- dc.js: Multiple graphs in a single dimension
- How to show the text of D3 node on hover?
- d3.js - How can I set the cursor to hand when mouseover these elements on SVG container?
- inner html from d.properties from 2 different data sources (d3.js)
- D3.JS Bubble Chart - reset chart when reducing circle size
- adding objects into an existing object
- How to draw clickable line in PIXI.js using PIXI.Graphics?